Thread: Files (reading from)

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    10

    Files (reading from)

    Sup guys! I recently started writing to files and was feeling pretty good about thing especially when your program finishes and my little text file pops up with the results from fprintf(). I'm doing fine with that. However I am having problems with reading from the file and I can't really understand the read from part. For instance if I have two words on a line like a firstname and a surname
    John Doe

    how do I read the John alone and then the Doe after? If anyone of you can just give me a hint on this I would really appreciate it. Thanks alot guys
    zbap

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Use this example, if you use fprintf, you will need to fscanf them.
    Code:
    FILE *filePtr;
    int blah = 1, temp;
    char blah2[30] = {"Butt"}, temp2[30];
    float blah3 = 5.5, temp3;
    
    //you would open the file at some point here. filePtr = fopen(...)
    
    fprintf(filePtr, "%d%s%f", blah, blah2, blah3);
    fscanf(filePtr, "%d%s%f", &temp, temp2, &temp3);
    
    printf("%d %s %f", temp, temp2, temp3);
    
    //and dont forget to close the file
    --edit---had a little typo.
    Last edited by stumon; 04-04-2003 at 10:48 AM.
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM