C Programming quick question

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]

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

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


#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 :wink:

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

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 :stuck_out_tongue: 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 :frowning:

Basically it executes the command


cp copy.c copy2.c

So it copies the first file into the second file.
Nice njs :wink:

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

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.

use an iteration of fgetc and fputc:

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.

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.

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


#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 :wink:

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

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.

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

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