Trying to read a line of records from a file?

Here is my code:

#include <string>
#include <fstream.h>
#include <windows.h>

using std::string;

void main()

      ofstream out;
      ifstream in;
      string sBuff; "btnbad.asc", ios::in, filebuf::sh_read ); "btnbad.txt", ios::out, filebuf::sh_write );

      while( in.eof() == false )

            // Code that reads in the line of records

            // Writes to output file
            out.write( sBuff.c_str(), lstrlen( sBuff.c_str() ));



I thinkg its but not quite sure how to use it.  I dont want to read a character at a time but instead a line of records which then goes to the next line, etc.  Do I also need something to move the pointer to the next record on the next row?

Who is Participating?
nietodConnect With a Mentor Commented:
You don't need to reject an answer if you just want more clarification from the expert.  Reject an answer only if it becomes apparent that the expert cannot help you.

All of this information woyuld have been good to knwo before!

I would use getline() to read a single line into an STL string.  Then I would sub-string this string into strings that contain each individual field.  Tnen convert those fields to the final necessary data types.  

example follows.
What is the format of the file.  is it binary?  is it ASCII?  What is stored in each record?  how are things delimited?

Assuming the data is stored in ASCII. You can use the >> operator to read the data, like

int x;
double y;

in >> x; // reads the integer.
in >> y; // reads the double.

Does that help?

To move to the next line, you can use

eric07Author Commented:
I'm not rejecting your answer b/c I think its wrong b/c I dont know, but b/c I want to explain my question more clearly.  Here is the records in the file (btnbad.asc) that I would read( all in ASCII):


I would like to read in a line of record one at a time b/c somewhere in the program I will have to break it apart for formatting later on.  And that is why I'm trying to figure out how to use the read and write, etc.

I'm use to using CStdio in MFC but that doesnt work for ATL.  I do have another question and that is I'm also use to using fopen, fclose,  fwrite, etc. And I wonder is there a penalty for using C functions in an ATL based project.  I've read that bypassing C functions the atl component is allot smaller and more robust.

eric07Author Commented:
One more thing and that is I know that what I posted is not an ATL program.  I wanted to just figure out how to use file io using C++ then I was going to add it in my ATL project.
Say for example the first 3 characters form a string that represent an integer, and the next 5 a string that represents a double you would do
string Lin;

string Fld1 = Lin.substr(0,2);
string Fld2 = Lin.substr(2,3);

int x;
double y;

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.