# C Programming quick question

**URL:** <https://forum.kirupa.com/t/c-programming-quick-question/184980>\
**Category:** random\
**Created:** [April 9, 2006, 11:51pm UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980 "2006-04-09T23:51:56Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![hsadan](https://avatars.discourse-cdn.com/v4/letter/h/7bcc69/32.png) [@hsadan](https://forum.kirupa.com/u/hsadan)\
**Post date:** [April 9, 2006, 11:51pm UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/1 "2006-04-09T23:51:56Z")

</div>

for my homework assignment I’m asked to get data from an input file, and then put it in an output file.

simple, but there’s one more thing:  
“do not store the information from the file inside the program. If necessary, read the file multiple times.”

Now, correct me if I’m wrong but whenever you use a fgets, fscanf etc I would have to store it somewhere, right?

Or is there a way I could get the data and put it directly into the output file?

My thinking is that the homework assignment is probably referring to storing all the data (as opposed to just something temporary for each scanned lined), because we’re also asked to sort the output, so some people may try putting everything and sorting it in the program.

[whisper]this should probably go into computer & games, but it’s kinda urgent and more people read random =P. If this shouldn’t be here, please feel free to move it[/whisper]

---

<div class="post-metadata">

**Author:** ![MTsoul](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@MTsoul](https://forum.kirupa.com/u/MTsoul)\
**Post date:** [April 10, 2006, 12:01am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/2 "2006-04-10T00:01:48Z")

</div>

You can technically store the data from fgets and fwrite it directly to a file by calling fgets as a parameter of fwrite.

---

<div class="post-metadata">

**Author:** ![955](https://avatars.discourse-cdn.com/v4/letter/9/4bbf92/32.png) [@955](https://forum.kirupa.com/u/955)\
**Post date:** [April 10, 2006, 12:05am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/3 "2006-04-10T00:05:35Z")

</div>

If your professor has a sense of humour, you could always do something like this:

```auto

#include <stdlib.h>
#include <string.h>

int main () {
        const char *infile = "copy.c";
        const char *outfile = "copy2.c";
        char *cmd = NULL;
        
        cmd = malloc (strlen ("cp ") + strlen (infile) + strlen (outfile) + 1);
        sprintf (cmd, "cp %s %s", infile, outfile);
        system (cmd);
        free (cmd);
}

```

You don’t even have to open the file, so I don’t think you’d lose marks for storing any data from the file in your program 😉

More seriously, why don’t you ask your teacher what it means? That’s what I’d do…

---

<div class="post-metadata">

**Author:** ![hsadan](https://avatars.discourse-cdn.com/v4/letter/h/7bcc69/32.png) [@hsadan](https://forum.kirupa.com/u/hsadan)\
**Post date:** [April 10, 2006, 12:10am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/4 "2006-04-10T00:10:24Z")

</div>

> [@MTsoul](#):
>
> You can technically store the data from fgets and fwrite it directly to a file by calling fgets as a parameter of fwrite.

Don’t think I can call fgets without specifying the string pointer argument.

[quote=λ]More seriously, why don’t you ask your teacher what it means? That’s what I’d do…[/quote]Interesting method you got there, but don’t think I’ve learnt that yet so it’ll take me a few more minutes to understand what you’re doing… but anyway, to answer your question:  
My assignment is due in 4 hours, and it’s a Sunday night 😛 I just realized this little tricky line they threw into the instructions.

edit: Ah I see what you’re doing:trout:. Nope, I dont think my prof would allow that to slide… after all, I should have clarified with her earlier ☹

---

<div class="post-metadata">

**Author:** ![Yeldarb](https://avatars.discourse-cdn.com/v4/letter/y/51bf81/32.png) [@Yeldarb](https://forum.kirupa.com/u/Yeldarb)\
**Post date:** [April 10, 2006, 12:30am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/5 "2006-04-10T00:30:56Z")

</div>

> [@hsadan](#):
>
> Interesting method you got there, but don’t think I’ve learnt that yet so it’ll take me a few more minutes to understand what you’re doing…

Basically it executes the command

```auto

cp copy.c copy2.c

```

So it copies the first file into the second file.  
Nice njs 😉

---

<div class="post-metadata">

**Author:** ![MTsoul](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@MTsoul](https://forum.kirupa.com/u/MTsoul)\
**Post date:** [April 10, 2006, 12:31am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/6 "2006-04-10T00:31:19Z")

</div>

Paste the instructions for the entire assignment. I’m not getting what you are saying ☹

---

<div class="post-metadata">

**Author:** ![hsadan](https://avatars.discourse-cdn.com/v4/letter/h/7bcc69/32.png) [@hsadan](https://forum.kirupa.com/u/hsadan)\
**Post date:** [April 10, 2006, 4:57am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/7 "2006-04-10T04:57:11Z")

</div>

Don’t worry about it MTsoul.

njs I need to the output file sorted… so that wouldn’t be enough :-/

I managed to get everything done, except that for some reason stdio.h does not seem to have popen and pclose, and I need those in order to print the date in my file too! Bah.

oh I know I could use time.h, but the date printed in the assignment example is in the format of when you use the “date” command.

---

<div class="post-metadata">

**Author:** ![thoriphes](https://avatars.discourse-cdn.com/v4/letter/t/9fc348/32.png) [@thoriphes](https://forum.kirupa.com/u/thoriphes)\
**Post date:** [April 10, 2006, 5:39am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/8 "2006-04-10T05:39:40Z")

</div>

use an iteration of fgetc and fputc:

```auto
FILE * infile = fopen(...), * outfile = fopen(...);
while (!feof(infile)) {
    fputc(fgetc(infile), outfile);
}
fclose(infile);
fclose(outfile);

```

I’d recommend opening the file as a binary.

---

<div class="post-metadata">

**Author:** ![hsadan](https://avatars.discourse-cdn.com/v4/letter/h/7bcc69/32.png) [@hsadan](https://forum.kirupa.com/u/hsadan)\
**Post date:** [April 10, 2006, 6:08am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/9 "2006-04-10T06:08:46Z")

</div>

ah, didn’t think of using fgetc/fputc .

that however, seems to only work if I were copying the file character for character. This isn’t all that I’m supposed to do… I need the output file sorted.

The input file contains a name and a year, and I’m supposed to sort them by the year.

This would be another problem … how would I make a comparison without temporarily storing at least 1 value?

edit: hm wait, I think that’s possible…just gonna get ugly.

---

<div class="post-metadata">

**Author:** ![955](https://avatars.discourse-cdn.com/v4/letter/9/4bbf92/32.png) [@955](https://forum.kirupa.com/u/955)\
**Post date:** [April 10, 2006, 11:37am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/10 "2006-04-10T11:37:01Z")

</div>

I know your assignment is probably in by now… but…

```auto

#include <stdlib.h>
#include <string.h>

int main () {
        const char *infile = "namesandyears";
        const char *outfile = "namesandyears-sorted";
        char *cmd = NULL;
        
        cmd = malloc (strlen ("sort -g -o ") + strlen (infile) + strlen (outfile) + 1);
        sprintf (cmd, "sort -g -o %s %s", outfile, infile);
        
        system (cmd);
}

```

Naturally, that won’t work on windows off the bat because it needs POSIX sort, but if you’re using Linux or OSX you’re fine 😉

---

<div class="post-metadata">

**Author:** ![thoriphes](https://avatars.discourse-cdn.com/v4/letter/t/9fc348/32.png) [@thoriphes](https://forum.kirupa.com/u/thoriphes)\
**Post date:** [April 10, 2006, 2:19pm UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/11 "2006-04-10T14:19:15Z")

</div>

> [@hsadan](#):
>
> ah, didn’t think of using fgetc/fputc .
> 
> that however, seems to only work if I were copying the file character for character. This isn’t all that I’m supposed to do… I need the output file sorted.
> 
> The input file contains a name and a year, and I’m supposed to sort them by the year.
> 
> This would be another problem … how would I make a comparison without temporarily storing at least 1 value?
> 
> edit: hm wait, I think that’s possible…just gonna get ugly.

hm…you can use fseek and ftell but that would require storing at least one value to make the comparison.

---

<div class="post-metadata">

**Author:** ![hsadan](https://avatars.discourse-cdn.com/v4/letter/h/7bcc69/32.png) [@hsadan](https://forum.kirupa.com/u/hsadan)\
**Post date:** [April 11, 2006, 3:53am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/12 "2006-04-11T03:53:37Z")

</div>

I managed to carry out comparison by directly comparing fgetc(infile) and fgetc(outfile).

I still have yet to figure out another problem that came from that, though, but it’s rather hard to type it out because I myself am confused and not sure where the problem is.

njs, interesting. I don’t know if I’m allowed to do that, heh. I’ll have to give that a shot.

---

<div class="post-metadata">

**Author:** ![Yeldarb](https://avatars.discourse-cdn.com/v4/letter/y/51bf81/32.png) [@Yeldarb](https://forum.kirupa.com/u/Yeldarb)\
**Post date:** [April 11, 2006, 4:01am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/13 "2006-04-11T04:01:32Z")

</div>

njs - I think the point is to program the algorithm, not use a premade one 😛

---

<div class="post-metadata">

**Author:** ![955](https://avatars.discourse-cdn.com/v4/letter/9/4bbf92/32.png) [@955](https://forum.kirupa.com/u/955)\
**Post date:** [April 11, 2006, 11:17am UTC](https://forum.kirupa.com/t/c-programming-quick-question/184980/14 "2006-04-11T11:17:07Z")

</div>

> [@Yeldarb](#):
>
> njs - I think the point is to program the algorithm, not use a premade one 😛

Well, obviously, but it’s gotta get originality points and thinking-outside-the-box points 😉
