help with file I/O

T

T.Crane

Hi,

I'm new to C++, and I want to open a file, go through it line by line
and pull out specific things on each line. I've written a test bit of
code to try to do this. For some reason what I'm doing doesn't work
-- it returns a runtime error and I don't know why. Obviously there's
something wrong with how I'm interpreting whatever is returned by the
getline function. Does the getline function not return a string? If
it does, why do I have trouble using string class methods
(like .at())? And if it doesn't return a string object, what does it
return and how do I search for a substring and then display it. The
code is below, and then after that is a test file. Any help you can
give me is much appreciated.

thanks!
trevis

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
ifstream myFile("test2.dat",ios::in);
if(!myFile)
{
cerr << "File could not be opened.\n";
exit(1);
}
string line;
size_t loc;
string temp;
while(getline(myFile,line)){
cout << line << setw(5) << line.size() << endl;
if(line.size()!=0){
loc = line.find("Az=",0);
temp = line.substr(loc,3);
cout << line << setw(5) << temp << endl;
}
}
myFile.close();
return 0;
}

The file it's supposed to work on (test2.dat) is below:

S/N 1111
06-08-2007, 10:43
Operator: John Doe
PASS

ANGLE: AZ=0, EL=0

RED
0.000000 0.000000 0.000000 0.000000
0.010000 0.000000 0.000000 0.000000
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi,

I'm new to C++, and I want to open a file, go through it line by line
and pull out specific things on each line. I've written a test bit of
code to try to do this. For some reason what I'm doing doesn't work
-- it returns a runtime error and I don't know why. Obviously there's
something wrong with how I'm interpreting whatever is returned by the
getline function. Does the getline function not return a string? If
it does, why do I have trouble using string class methods
(like .at())? And if it doesn't return a string object, what does it
return and how do I search for a substring and then display it. The
code is below, and then after that is a test file. Any help you can
give me is much appreciated.

thanks!
trevis

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
ifstream myFile("test2.dat",ios::in);
if(!myFile)
{
cerr << "File could not be opened.\n";
exit(1);
}
string line;
size_t loc;
string temp;
while(getline(myFile,line)){
cout << line << setw(5) << line.size() << endl;
if(line.size()!=0){
loc = line.find("Az=",0);

What happens if the line does not contain "Az="? I believe that loc will
be npos, and I don't think that substr() works well with that as a
start value. Add a check here.
 
M

Meal

Hi,

I'm new to C++, and I want to open a file, go through it line by line
and pull out specific things on each line. I've written a test bit of
code to try to do this. For some reason what I'm doing doesn't work
-- it returns a runtime error and I don't know why. Obviously there's
something wrong with how I'm interpreting whatever is returned by the
getline function. Does the getline function not return a string? If
it does, why do I have trouble using string class methods
(like .at())? And if it doesn't return a string object, what does it
return and how do I search for a substring and then display it. The
code is below, and then after that is a test file. Any help you can
give me is much appreciated.

thanks!
trevis

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
ifstream myFile("test2.dat",ios::in);
if(!myFile)
{
cerr << "File could not be opened.\n";
exit(1);
}
string line;
size_t loc;
string temp;
while(getline(myFile,line)){
cout << line << setw(5) << line.size() << endl;
if(line.size()!=0){
loc = line.find("Az=",0);
temp = line.substr(loc,3);
cout << line << setw(5) << temp << endl;
}
}
myFile.close();
return 0;

}

The file it's supposed to work on (test2.dat) is below:

S/N 1111
06-08-2007, 10:43
Operator: John Doe
PASS

ANGLE: AZ=0, EL=0

RED
0.000000 0.000000 0.000000 0.000000
0.010000 0.000000 0.000000 0.000000

Read the getline function's declaration, you'll know that it won't
return false even if going to the file end.

template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str
);

template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str,
CharType _Delim
);
 
M

Marcus Kwok

Meal said:
Read the getline function's declaration, you'll know that it won't
return false even if going to the file end.

template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str
);

However, it returns a reference to the istream. If getline() reaches
the end of the file, then when the reference is converted to a bool, it
will terminate the loop.

See this entry in the FAQ:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.4

(it talks about operator>> but the idea is the same).
 
J

Jim Langston

T.Crane said:
Hi,

I'm new to C++, and I want to open a file, go through it line by line
and pull out specific things on each line. I've written a test bit of
code to try to do this. For some reason what I'm doing doesn't work
-- it returns a runtime error and I don't know why. Obviously there's
something wrong with how I'm interpreting whatever is returned by the
getline function. Does the getline function not return a string? If
it does, why do I have trouble using string class methods
(like .at())? And if it doesn't return a string object, what does it
return and how do I search for a substring and then display it. The
code is below, and then after that is a test file. Any help you can
give me is much appreciated.

thanks!
trevis

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
ifstream myFile("test2.dat",ios::in);
if(!myFile)
{
cerr << "File could not be opened.\n";
exit(1);
}
string line;
size_t loc;
string temp;
while(getline(myFile,line)){
cout << line << setw(5) << line.size() << endl;
if(line.size()!=0){
loc = line.find("Az=",0);

if ( loc != std::string::npos )
> temp = line.substr(loc,3);
else
temp = "Not found";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top