#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0

    Reading Text, then outputting.


    I tried searching through the topics, but nothing really matched what I was looking for.

    I must admit, i'm a newbie at this - i've only been at it for about a few weeks now, and we've been given a project to do - however, it's been hindered by one problem.

    We need to read information off a text file, and then out put it onto the dos display and simulataneously on to an output text file.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
     
    int main()
    
    {
    	ifstream inData;
    	ofstream outData;
    
    	inData.open("C:netpay.txt");
    	outData.open("C:netpay.txt");
    
                    inData.get();  //problem right here
    
    	inData.close();
    	outData.close();
    	
    	return 0;
    
    }
    The problem is that, after I get the information, how do I use it? As in, how can I get the data to actually transfer to another file, or to use it in another function? I also noticed that the data from the .txt disappears after running the program - how can I prevent this? :confused: I don't even think i'm using the proper 'get' function to get a whole line.

    Thanks for the help in advance.
    Last edited by Liu; July 3rd, 2003 at 12:50 AM.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0
    EDIT: scratch that.. still the same problem.
    Last edited by Liu; July 3rd, 2003 at 01:34 AM.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    First, I admit that I'm much more familiar with standard C I/O functions than with iostreams, but I might still be able to point you in the right direction.

    I believe that you want to use getline() instead of get:
    istream& getline( char* pch, int nCount, char delim = '\n'_);

    However, get() should have worked for you if you had given it the parameters it needed:
    istream& get( char* pch, int nCount, char delim = '\n'_);

    When you "get" something, you also need to put it somewhere. For that, you need to declare a character array, AKA a "buffer", to hold the data that you get. That array must be large enough to hold an entire line plus one character (that extra character being the null-terminator that marks the end of the string). In the two parameter lists given above, pch is the name of the buffer that you have declared and nCount is the maximum number of characters that you will read in (should be buffer size minus one). You do not need to include the third parameter, which defaults to "new-line".

    After the get/getline, buffer now contains the line that was read in. Now you can output the contents of buffer to the display screen and to the output file. Then you get/getline the next line in the input file and repeat until you have read and processed the entire input file.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    56
    Rep Power
    12
    You take file input just like screen input. I'm a little rusty, so check your book for correct syntax, but this looks about right. I did a quick google search for you to read if this doesn't sink in.

    http://cplus.about.com/library/weekly/aa051802a.htm

    eg:
    Code:
    ifstream fin;
    ofstream fout;
    int n;
    char str[81];
    char chr;
    
    fin.open("myfile.txt");
    if (!fin)
    {
       //output error msg and exit
    }
    
    fout.open("results.txt", ios::app);
    if (!fout)
    {
       //output error msg and exit
    }
    
    // File streams are handled just like IO streams
    fin >> n;
    fout << n << "\n";
    cout << n << endl;
    
    // You can use the getline function to get a line of characters.  
    // I set my limit to 80 to have room for null terminator
    fin.getline(str, 80);
    fout.write(str, 80);  //I'm a little rusty on this
    cout << str << endl;
    
    
    
    //or using get
    while (fin.get(chr))
    {
       fout.put(chr);
       cout << chr;
    }
    
    
    fin.close();
    fout.close();
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0
    Thanks for the help :)

    After doing some late night reading after I made the post, I was straying towards using a buffer and using getline; so I came up with this:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
     
    int main()
    
    {
    	char buffer[256];
    	
    
    	//declaration of file stream variables
    	ifstream inData;
    
    	//open files
    	inData.open("C:input.txt");
    
    	//executes if open fails.
    	if (!inData)
    	{
    		cout<<"Open input file failure"<<endl;
    		exit (1);
    	}
    	
    	while(!inData.eof())
    	{
       	inData.getline (buffer, 256);
    	cout<<buffer<<endl;
    	}
    
    	//Close file
    	inData.close();
    	
    	return 0;
    
    }
    However, the reason why I didn't want to use the getline function is because the text file has a mixture of names and numbers (how much they earn in $$); so I wanted to somehow store their names separately from storing the numbers; so I can work with the numbers. Is there a way of splitting the the information after you use getline; or is there another 'get' function where it gets a row of characters, and stops at a white space.

    So it'd resemble something like a user input (which is really what i'm looking for):

    Code:
    cin>>a>>b>>c;
    where the user input is:

    first middle last
    Last edited by Liu; July 3rd, 2003 at 11:51 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    56
    Rep Power
    12
    You can use getline. The parameters for getline are istream& getline (char* s, streamsize n, char delim );

    edit: This would work the best (I found an old program to look at, look for example below):
    Alternatively you may also just want to try inData >> str1 >> str2 >> str3 >> int1;
    I haven't done this for awhile and I don't have any old code to look at atm or else I'd give you a definite yes/no. The below would work though.


    Let's say your file contains something like this:
    Calvin Henry Hobbes 2500.45

    then you're code would look something like this:
    Code:
    char firstname[81], middlename[81], lastname[81];
    float cash;
    
    while(!inData.eof())
    {
       	inData.getline (firstname, 80, ' ');
    	inData.ignore(1);  // to ignore our space
            inData.getline (middlename, 80, ' ');
            inData.ignore(1);
            inData.getline (lastname, 80, ' ');
            inData.ignore(1);
            inData >> cash;
            inData.ignore(1); //ignore \n
            cout << firstname << " " << middlename << " " << lastname << " earns " << cash << " dollars." << endl;
    }
    or

    Code:
    char firstname[81], middlename[81], lastname[81];
    float cash;
    
    while(!inData.eof())
    {
       	inData >> firstname;
            inData >> middlename;
            inData >> lastname;
            inData >> cash;
            cout << firstname << " " << middlename << " " << lastname << " earns " << cash << " dollars." << endl;
    }

    Would output:

    Calvin Henry Hobbes earns 2500.45 dollars.

    So yes, you can do exactly like a cin >> a >> b >> c;
    Last edited by helado; July 3rd, 2003 at 12:12 PM.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0
    Thank you!

    I don't know how I overlooked that inData>>a>>b>>c can be used like cin>>a>>b>>c - must've been the lack of sleep :\

    Now I can get on with the rest of the program. :)

IMN logo majestic logo threadwatch logo seochat tools logo