This site has helped me a lot regarding some programming techniques.
I would like to ask another help.
This is about reading a "txt file" and storing the contents in a variable.
But i've got problems with it because normally, it is space delimited, that is, values separated by spaces are considered different values.
How am I going to make C++ read the whole line including spaces?
Here's part of that code. The variable expstr is of type character, all through out the program. So definitely, it will kill me if I have to change all to string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
ifstream inputdata;
inputdata.open("C:/Users/Engr. Jimmy Neutron/Documents/C++/Parser/Parser/Parser.txt",ios::in|ios::out); // opens the file
if(!inputdata) /* if file could not be opened */
{
cerr << "Error: file not found or could not be opened" << endl;
abort();
}
while (!inputdata.eof())
{
inputdata >> expstr;
}
Perhaps sharing the actual type of expstr would prove productive. There is no type character.
Lines 12 through 15 are pretty nonsensical unless you only want the last bit of the file. You keep overwriting expstr, whatever it's actual type may be.
yeah, maybe you're right. I think, it is around 1000 array character.
I used this algorithm when I am working with Mathematics of matrix, so its kinda bit useful in this case. Maybe, I'll work on changing it..
@MiiNiPaa
I'll take try this suggestion and improve on how it will work..
Thank you sirs. :) I will give feedback later. :)
Feedback:
@MiiNiPaa, Yes! it's working. :) Thank you sir..
Can i have this code instead?
1 2 3 4 5 6 7 8 9 10
if(!inputdata) /* if file could not be opened */
{
cerr << "Error: file not found or could not be opened" << endl;
abort();
}
else
{
inputdata.getline(expstr, 9999);
}
If expstr is less than 9999 characters in size and single line is larger than expstr size, you will get buffer overflow, undefined behavior and all kind of nasty things happens. This is what you get when working with c-strings: you must make sure that you provide correct buffer size yourself.
@MiiNiPaa
Got that sir, I once have that problem and it results to errors in output.
Not to worry because "normal" linear/non-linear equations doesn't usually exceed 10,000 characters.
Or I am thinking that the
inputdata.eof()
must always be checked? Am i getting to your point?
Not to worry because "normal" linear/non-linear equations doesn't usually exceed 10,000 characters.
You previously said that your char array is 1000 characters long.
And it is completely possible that file created in Linux will be opened under Windows. Linux style newlines are not recognised by Windows, so whole file would be read as one line.
Do not take chances, provide correct sizes. Or use C++ managed containers.
Or I am thinking that the inputdata.eof()
must always be checked? Am i getting to your point?
.eof() is largely useless and is good only to get cause of failed read.
You should loop on/check stream state after read instead:
Oops, sorry. A bit of misunderstanding.
What i mean is that I declared the variable as an array of size 10,000
but then you are just going to put, for example, a string of size 55. Will there be problems?