954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

fstream problem.

Hey,

I'm trying to only read the int's and the symbol - (negative) from the file.

right now my code is where m,d, and y are dates.
Everything works fine. Except I do not read the - symbol.

Any one know how to solve this?


fin.open(ifile.c_str());
fin >> m1 >> c >> d1 >> c >> y1;
fin >> m2 >> c >> d2 >> c >> y2;
fin >> m3 >> c >> d3 >> c >> y3;
fin.close();


For example date -10/-1/1999

will be read as 10/1/1999

for my assignment I must read the negative symbol.

Thanks

imput1234
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

What are the data types of the variables?

With the mdy variables as ints and the c as a char, it seems to work for me - I get the negative values.

A full code sample illustrating your problem will help us help you.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

Nevermind, weird it works now. I havent;' made any changes.

But I do have another problem.

I'm trying to make the program so that, if the user does not enter a valid file name, it will output that and clsoe the program.

This is what I have so far, and no matter what the user enters, the console will go in to a failstate. Anyone know why

Thanks.

ifstream fin;

cout << "Enter filename: ";
getline(cin, ifile);


if ( !ifile.size() == 0 )
{
cout << "No file found." << endl;
system ("PAUSE");
return 1;
}

fin.open(ifile.c_str());

imput1234
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

That should work. Please post the exact data file contents you're trying to read.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

edited original post.

ifile is a string.

imput1234
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

I've also tried


if ( !ifile.empty() )
{
cout << "No file found." << endl;
system ("PAUSE");
return 1;
}

imput1234
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

And, the test you're doing is only testing the filename the user entered, it does not test if there's actually a file or if it has any content.

(this should have come after my next reply - they got out of order somehow)

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

You're asking a question that's not exactly what I think you mean.

if ( (! ifile.size( ) ) == 0 ) means, logically negate the value the size function returns, giving you a boolean true or false value, then compare that to zero, which is considered false.

If you're trying to find out if the user actually entered some string as the file name, try

if( ifile.size( ) == 0 )
{
   //error handler
}
else
{
    fin.open( ifile.c_str( ) );
}
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

If failed to open..

if (fin.fail())
   cout<<"file does not exist";
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

I've tried those. They don't work.
This is my problem.
Lets say the user enters hello.txt
but hello.txt does not exist.
How can I make the program say "Does not exist"
That is what I have been trying to do.
all the previous methods don't do that, they just make the dates really long numbers.

Hope that makes more sense.

imput1234
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Still can't figure it out.

imput1234
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

What about..this...

//...
fin.open(ifile);
if(fin.fail())
   std::cout<<"does not exist";
//...
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

every text should cover this

fin.open ( "somefile.txt" );
if( !fin )  // or   if( fin.fail( ) )
{
     //error handler  goes here
}
else
{
    //file opened successfully, start reading
}
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

Nevermind, weird it works now. I havent;' made any changes.

But I do have another problem.

I'm trying to make the program so that, if the user does not enter a valid file name, it will output that and clsoe the program.

This is what I have so far, and no matter what the user enters, the console will go in to a failstate. Anyone know why

Thanks.

ifstream fin; cout << "Enter filename: "; getline(cin, ifile); if ( !ifile.size() == 0 ) { cout << "No file found." << endl; system ("PAUSE"); return 1; } fin.open(ifile.c_str());


just try this one........................... ifstream fin;

cout << "Enter filename: ";
getline(cin, ifile);
fin.open(ifile.c_str());

if(fin.fail())
{
cout << "No file found." << endl;
system ("PAUSE");
return 1;
}

fin.open(ifile.c_str());

rajenpandit
Newbie Poster
14 posts since Aug 2008
Reputation Points: 9
Solved Threads: 1
 

Hey guys,

I'm working on another program, and have run in to another problem.

Basically for this program, I read and extract information from a file. I do not know when the file is going to end, so I used a while loop to get my data.

This is all I have done, haven't proceeded since the loop doesn't work.
Hey guys,

I'm working on another program, and have run in to another problem.

Basically for this program, I read and extract information from a file. I do not know when the file is going to end, so I used a while loop to get my data.

This is all I have done, haven't proceeded since the loop doesn't work.


void extract ( string file_name, ifstream& fin )
{
int i = 0;
string temp;


fin.open( file_name.c_str( ) );
fin >> temp;
cout << temp;

while ( !fin.eof( ) )<strong>//THIS PART DOES NOT WORK I've also tried while (fin) while (!fin.fail( )) can't seem to get it to work</strong>
{
//STUFF

}

What I want it to do is end the program, when there is no more info in the file.

Thanks.

imput1234
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

alright I figured that out.

It seems as though that method will only work when followed by

fin >> somevariable;

is there anyway I can use getline (fin, string);

instead?

Thanks.

imput1234
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

while ( getline ( fin, str ) )
{
//do any other processing
}

If the read action is successful, you enter the loop body. If the read action fails, loop ends.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

I'm seem to have a LOT of problems with this program.
The loop issue is fixed, now it will not read all the numbers that exist.

The Main:

string file1, temp[1000];
float n1[1000], n2[1000];
int i = 1, t;

ifstream fin;
ofstream out;

cout << "\t\t Welcome to Grade Calculator!\n\n";
cout << "What file has your scores on it: ";
getline( cin, file1 );

fin.open( file1.c_str( ) );
out.open( "results.html" );

if ( fin.fail( ) || file1.size( ) == 0 )
{
cout << "No file was found or entered." << endl;
system ("PAUSE");
return 1;
}

out << "<html>" << endl;
out << "<body>" << endl;
out << "<pre>" << endl;
out << "\t\tGrade Report" << endl;
out << "Activity Your Grade Possible" << endl;

while ( getline ( fin, temp[i] ) )
{
fin >> n1[i] >> n2[i];
i++;
}

while ( i != 0 )
{
if (temp[i] == "end")
{
out << "Percentage" << setw(13) << getpart(fin, temp[i-1].substr(0, ' ')) << endl;
}
else if ( n1[i] != -1 || n2[i] != -1 )
{
out << setw(11) << temp[1] << setw(14) << n1[i] << setw(16) << n2[i] << endl;
}

i--;
}

out << "</html>" << endl;
out << "</body>" << endl;
out << "</pre>" << endl;

fin.close( );
out.close( );

system ("PAUSE");

return 0;

The getpart function float getpart (ifstream& fin, string label)
{
string temp;
float score = 0, total = 0, average, tem, n1, n2;

while (getline ( fin, temp ) )
{
if (temp != "end")
if (temp.substr( ) == label)
{
fin >> n1 >> n2;
score = score + n1;
total = total + n2;
}
}

average = score / total;

return average;
}

The file it is reading from Quiz 1
3 5
Quiz 2
4 10
Attd. 11-23
10 10
Quiz 3
5 5
end
-1 -1
Lab 1
10 100
Lab 2
20 20
Lab 3
30 130
end
-1 -1
Test 1
145 150
Test 2
200 210
end
-1 -1
Program 1
40 50
Program 2
90 100
Program 3
100 100
Program 4
90 100
end
-1 -1
Lab Test 1
35 50
Lab Test 2
25 50
end
-1 -1
Final Exam
234 400
end
-1 -1

Basically what the program does is organize the grades in another file .html
ie
Quiz 1 score total
and then calculate the percent in the end of each section
Percent: 100

imput1234
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

I ran some tests, and it looks like the array is only storing 2 variables, of which the first one is correct the second is wrong.

this is the part where I actually store information, anyone know whats wrong?

Thanks.

while ( getline ( fin, temp[i] ) )
{
fin >> n1[i] >> n2[i];
i++;
}

imput1234
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You