Reading a line of text with the spaces

Hi guys,

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?

Thanks for any idea / suggestions. :)
I imagine getline would do the trick.
use getline:

1
2
3
4
5
ifstream file("yourfile.txt");
string line;

// get the full line, spaces and all
getline(file,line);
Thank you for the reply. :)

But if I would want to store the "string" to a character-type variable, would it still be possible?
Last edited on
But if I would want to store the "string" to a character-type variable, would it still be possible?
You mean in character array? Yes, this is possible, but there is no reason to do so in C++.

If you still want to do it: http://en.cppreference.com/w/cpp/io/basic_istream/getline
Waaaaahhhh. Can't understand. :(

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.
inputdata.getline(expstr, <size of expstr here>)
@cire,

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);
	}
Last edited on
Can i have this code instead?
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:
1
2
3
4
5
6
7
8
9
10
11
constexpr std::size_t count = 1000;
char exprstr[count];
while( inputdata.getline(expstr, count) )
    std::cout << expstr << '\n';

if (inputdata.eof())
    std::cout <<  "Whole file was read\n";
else if(inputdata.fail() && inputdata.gcount() == count - 1)
    std::cout << "Encountered unusually large line\n";
else
    std::cout << "Unknown error\n";
Last edited on
@MiiNiPaa

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?
No, there would be no problem if line length is less than size of buffer.
Yosh!! I learned a lot.

I'm not that good in programming because I am a Civil Engineer. But i understood what you taught to me Sir MiiNiPaa. :)

Thank you so much. :)
Your help did so much to me this day. :) :) :)
Last edited on
Topic archived. No new replies allowed.