Thread: Reading in strings from a file..

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    2

    Reading in strings from a file..

    Ok, I don't knwo how to set a delimiter, so that it stops reading a string. I have read in name, address etc, from a file, but am not sure how to get it to stop after the first name. Can you guys tell me how to do that? here is the function.

    void addressBookType::readFile() //reads the data from a file using object Book of extPersonType
    {
    ifstream inFile;
    ofstream outFile;
    string first, last, adr, cit, st, member;
    int size, month, day, year, phone;
    double z;

    inFile.open("c:/test.txt");
    for(size = 0; size <= 5; size++)
    {

    inFile>>first>>last>>adr>>cit>>st>>z;
    inFile>>month>>day>>year;
    inFile>>member>>phone;
    Book[size].setPersonType(first, last, month, day, year, adr, cit, st, z, member, phone);
    }
    inFile.close();
    }

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    inFile >> firstName;

    it stops after reading the first name
    i read this somewhere i think..it inputs a set of character until a whitespace occurs like tab, newline, or space

    pretend it has a file named blah.txt that has this
    "someone likes
    me too"

    inFile >> string;
    that will store "someone" into string, if you do it again
    inFile >> string2
    that will store "likes" into string2

    also i read somewhere about the tellg(), tellp(), seekg(), seekp() functions..that tells current pos of the pointer to a certain character...changes it..gets it..or moves it..
    nextus, the samurai warrior

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    2
    so basically put a colon after each? I'm not sure if that will work but ill try.

    the file is set up like this... so it reads them... in this order
    J
    ohn Smith
    2504 Maple Tree
    Dallas, TX
    2 27 82
    Family
    5555555555

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    ouch..when it reads in name.."John" is one string, "Smith" is another...and the address is 3 strings...you have to contatcenate them..and make code that does that
    nextus, the samurai warrior

  5. #5
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    yes i found a way...stupid me...hehe..ok the fstream class has a function called getline()

    let me demonstrate..pretend i have a file name "test.txt" with the following stuff in it
    Code:
    cpp ninja rocks!
    cpp ninja is cool!
    with this code it will get the first line which is "cpp ninja rocks!" and stores it into name and outputs it

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    int main()
    {
    	char* name = new char[50];
    
    	ifstream a_file("test.txt", ios::nocreate);
    	a_file.getline(name, 50, '\n');
    
    	cout << name;
    
    	return 0;
    }
    hope that helps
    nextus, the samurai warrior

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with reading strings from a file
    By ldcel in forum C Programming
    Replies: 3
    Last Post: 12-01-2007, 01:31 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM