file input/output

This is a discussion on file input/output within the C Programming forums, part of the General Programming Boards category; trying to write a file with two user prompts i.e. int code & string title form this i wish to ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    Question file input/output

    trying to write a file with two user prompts i.e.
    int code & string title

    form this i wish to be able to read back the same information from the file. i can write the file however i cannot read back the file information one line at a time.


    e.g.

    0001 shriek
    0002 starwars
    etc;

    can anyone help please........
    sean.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    How did you open the file? I think you opened it as write-only, you should open it for read-write.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Place them in an array of structures to do it, opening the file in the binary read/write mode. Use fwrite and fread.
    Otherwise use a text file and open it in the standard read/write mode, and use fprintf and fscanf.

    this loop should work, for instance, on a text file, with an array of ints and strings:

    int code[size];
    char title[size][length];
    int x = 0;

    while(!foef(fp) && x++ < size)

    fscanf("%i %s", &code[x], &title[x]);
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    An array of structs or even a linked list would work.
    struct data { char line[LEN]; } array[SIZE];
    or
    struct data { int code; char title[LEN]; } array[SIZE];

    Read from the file until you don't have any more lines:
    Code:
    while ( fgets( array[i].line, sizeof( struct data ), IN ) != NULL ) {
      /* Parse the line and process */
      i++;
    }
    or
    while ( fread( array[i], sizeof( struct data ), 1, IN ) != 0 ) {
      /* Process each struct as you read it */
      i++;
    }
    Try to stay away from using feof() as your conditional for stopping the loop, you'll end up reading one line too many and the last struct will probably be garbage.

    Too tired, need sleep...

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21