Thread: question about reading files

  1. #1
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    Question question about reading files

    If I number everything in a .txt file how would I get the computer to read a character string after a certain number?
    e.g
    "Text file.txt"
    200 james
    201 darren
    202 john

    How do I get the computer to run thorugh the text file, find 201 and then read in 'darren'.

    James
    My site to register for all my other websites!
    'Clifton Bazaar'

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    The easiest way is when you creat your outfile, just have the number printed on the line directly above the name.
    Ex:

    //...
    ofstream outfile;
    outfile.open("textfile.txt");
    outfile << "200" << '\n'; //or substitute 200 with your variable
    outfile << "John" << '\n'; //same as above
    outfile.close();
    //...

    Then, when loading the file, you would do:

    //...
    int num;
    char name;
    ifstream infile;
    infile.open("textfile.txt");
    infile >> num; //gets 200 or the number into variable num
    infile >> name; //gets John or the name into variable name
    infile.close();
    //...

    You could loop that or whatever if you are using arrays, but that is the easiest way to do it.
    Last edited by Vegettų; 03-04-2002 at 07:25 PM.
    C:\DOS\
    C:\DOS\RUN\
    \RUN\DOS\RUN\

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Oops, I didn't answer your second question.

    You would need to create a loop that searched the entire file until it found it. You would need an if statement something like the following:

    //...
    int num;
    int done = 0;
    char name;
    ifstream infile;
    infile.open("textfile.txt");
    while((done = 0)||(!infile.eof())) { /*repeats until the end of the file is reached or it finds the correct number */
    infile >> num;
    infile >> name;
    if(num == 201) {
    done++;
    }
    }
    infile.close();
    //...

    Variable name is now containing the name you were searching for, and variable num contains the number at which it was found.
    C:\DOS\
    C:\DOS\RUN\
    \RUN\DOS\RUN\

  4. #4
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    thanks for that
    My site to register for all my other websites!
    'Clifton Bazaar'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about IOStream and reading strings from files
    By kingpinzs in forum C++ Programming
    Replies: 22
    Last Post: 12-13-2005, 11:29 AM
  2. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  3. Question About Reading Files
    By Zildjian in forum C Programming
    Replies: 1
    Last Post: 09-28-2003, 05:31 PM
  4. Reading files in a directory
    By roktsyntst in forum Windows Programming
    Replies: 5
    Last Post: 02-07-2003, 10:04 AM
  5. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM