quick question, please read

This is a discussion on quick question, please read within the C++ Programming forums, part of the General Programming Boards category; I have a lab where I read in a bunch of cities from a text file, sort them alphabetically in ...

  1. #1
    Unregistered
    Guest

    quick question, please read

    I have a lab where I read in a bunch of cities from a text file, sort them alphabetically in a linked list, then save the list as a new text file. I understand the linked list part and the sorting but how would I save to a new text file??

    Any help is appreciated.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Assuming you have a linked list, use the FILE family of functions ( fopen, fprintf, fclose ) to open the new file, print each node of your linked list and close the file.

    FILE* fp = fopen( "file.txt", "w" );

    while( NodesInList )
    {
    fprintf( fp, "%s", Node.Text );
    }

    fclose( fp );
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Unregistered
    Guest
    there are two ways to handle files in C++. The C_style way of handling files using the FILE functions as discussed by nvoigt is still valid, and prefered by many. Many others prefer the to use streams, which is available in C++ but not C. You can either use an fstream to read and write to file, or an ifstream to read from file and an ofstream to write to file.

    To use a file stream you need to list the fstream header file in your programs #include list. Then you declare a stream, open it, and associate it with a file. If the file is in the same directory as the executable function of your program, then you can use just the file name and extension to indicate which file you are associating with the stream. Otherwise you need to use the full path name, using double backslashes instead of single backslashes in the name. Streams have default behaviors that can be overridden with stream modifiers, look them up in your favorite textbook/tutorial. Below is an example of the "simplist way" to set up a set of dedicated streams to read from and write to a file called myFile.txt using default ifstream and ofsteam mode(s).

    ifstream fin("myFile.txt);
    ofstream fout("myFile.txt);

    File streams have all the same methods/modifiers as istreams and ofstreams plus a few extras. This means you can read in a string without whitespace using >> and can read in a string with whitespace with getline(), etc.

    IF you are writing/reading structs/classes to a file you can read/write all data members for a given instance using the read() and write() stream methods. However, these methods create binary files, not text files, so you can't review them in the file itself.

    When you write pointers to file, they become worthless on print out as the address printed to file will in all probability not be the one pointing to a given variable the next time you read in data from the program. Therefore, pointers are often not written to file, the value(s) in the object at the address they point to in the current program is what is written and read from file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05:39 AM
  2. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  3. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM
  4. Quick Question.
    By SavesTheDay in forum C Programming
    Replies: 3
    Last Post: 04-23-2002, 11:09 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

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