Reading Lines from a file

This is a discussion on Reading Lines from a file within the C++ Programming forums, part of the General Programming Boards category; Hey everybody, I want to be able to read lines of data from a text document and store it in ...

  1. #1
    Registered User
    Join Date
    Dec 2005

    Reading Lines from a file

    Hey everybody, I want to be able to read lines of data from a text document and store it in a array and output the array in a file. does know how to skip whitespace when reading data from a file. The data i want to read is below

    Forsyth, Frederick ;The fist of god ;A;102391
    Follett, Ken ;The pillars of the earth ;A;103795
    Francis, Clare ;Wolf winter ;A;112424
    Smith, Wilbur ;A falcon flies ;A;151395
    Forsyth, Frederick ;The day of the jackal ;A;159151
    Blackmore, R.D. ;Lorna Doone ;A;193292
    Smith, Wilbur ;Men of men ;A;292351
    Smith, Wilbur ;The angels weep ;A;482315
    James, P.D. ;Cover her face ;D;144371
    James, P.D. ;Devices and desires ;D;193395
    Kellerman, Faye ;Stone kiss ;D;434517
    James, P.D. ;The skull beneath the skin ;D;439518
    Kelly, Jim ;The water clock ;D;543951
    Hoag, Tami ;Dark horse ;D;664333
    Cookson, Catherine ;The silent lady ;R;151611
    Cookson, Catherine ;Kate hannigans girl ;R;165163
    Thompson, E.V. ;The lost years ;R;165213
    Steel, Danielle ;The cottage ;R;177516
    Binchley, Maeve ;Quentins ;R;261561
    Alcott, Louisa ;Little women ;R;395104
    Cookson, Catherine ;Rosie of the river ;R;512843
    Asimov, Isaac ;Foundation ;S;165108
    Barker, Clive ;Imajica ;S;172816
    Barker, Clive ;The great and secret show ;S;174427
    Barker, Clive ;Weaveworld ;S;282118
    Clarke, Arthur C ;2001 A space odessy ;S;294561
    Asimov, Isaac ;Foundation and empire ;S;313178
    Asimov, Isaac ;Second foundation ;S;549201
    And here is the code i have done so far. what i am doing wrong.
    #include <string>
    #include <fstream.h>
    #include <assert.h>
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    const int max = 5000;
    struct book // holds book details
      string secondname;
      string firstname;
      string title;
      char cat;
      int bnumber;
    int main (int argc, char* argv[])
      assert (argc >= 1); // type book data filename
      ifstream accfile (argv[1]); //opens book data
      book b[max];
      int counter = 0;
        accfile >> b[counter].secondname >> b[counter].firstname
                >> b[counter].title >> b[counter].cat >> b[counter].bnumber //stores it in array b   
        if (accfile)
      } while (accfile && counter < max);
     cout << counter << " read" << endl;
     ofstream oaccfile("search.txt");
     // write data to a new file
     for ( int i = 0; i < counter; i++ )
         oaccfile << b[i].secondname << " " << b[i].firstname << " " << b[i].title
    	      << b[i].cat << " " << b[i].bnumber << endl;
     oaccfile.close() ;
     cout << "Output file created: Seacrh.txt" << endl ;

  2. #2
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    you could us fgets to read the entire line and output that to the file..

    i could do this in C, but i always had problems with fstream myself, lol
    but you are missing a ; on line 26..
    Registered Linux User #380033. Be counted:

  3. #3
    Registered User
    Join Date
    Jan 2005
    >> what i am doing wrong.

    First, it should be <fstream>, not <fstream.h>. Second, as willc0de4food mentioned you are missing a semicolon. Once you fix these two things and the code compiles, I assume you are having trouble reading in the data in its current format.

    What you should use is getline (not fgets) to read in part of the line without stopping at whitespace. To get the secondname, you would read until you found a comma, which can be done with getline(accfile, b[counter].secondname, ','). The first name would be up to the next semi-colon, so you would do something similar: getline(accfile, b[counter].firstname, ';').

    You could do the same for the title, and then use operator >> to read in the category char and the bnumber int. If you sprinkle in a few accfile.get() calls to ignore characters you don't need (like the semicolon between the category and the bnumber or the newline at the end of each line), then you will have it working.

    So play around with getline, operator>> and get. If you can understand how they work it won't be too difficult to combine them to get all the data from the file correctly.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Thank you willc0de4food and Daved for your help
    i didn't expect such a quick response for my Thread

    The code works fine thanks to both responses.

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. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Reading file into edit - losing new lines
    By eam in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2003, 12:07 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