Skip to content Skip to sidebar Skip to footer

Read From Text Split by Space in C

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.
Simply 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 exercise 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. :)

Only if I would want to store the "string" to a character-type variable, would information technology still be possible?

Terminal edited on

Waaaaahhhh. Can't understand. :(

Here's function of that code. The variable

expstr

is of type character, all through out the program. So definitely, information technology will impale me if I take to change all to cord.

                          1
2
iii
4
five
vi
7
8
9
10
xi
12
13
14
xv
                          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 <<                            "Mistake: file not establish or could not be opened"                            << endl; 				arrest(); 			}                            while                            (!inputdata.eof()) 			{ 				inputdata >> expstr; 			}                        

Perhaps sharing the bodily type of

expstr

would evidence productive. There is no blazon

grapheme

.

Lines 12 through xv are pretty nonsensical unless yous only desire the terminal flake of the file. You keep overwriting

expstr

, whatever information technology'south actual type may be.

inputdata.getline(expstr, <size of expstr here>)

@cire,

yeah, maybe you're right. I call back, it is around grand array character.
I used this algorithm when I am working with Mathematics of matrix, and so its kinda bit useful in this example. Maybe, I'll work on changing it..

@MiiNiPaa
I'll accept attempt this proposition and improve on how information technology will work..

Thank you lot sirs. :) I will requite feedback later. :)

Feedback:
@MiiNiPaa, Yes! information technology's working. :) Give thanks you sir..

Can i accept this code instead?

                          ane
two
3
4
5
half dozen
7
8
9
ten
                                                      if(!inputdata)                            /* if file could non exist opened */                            { 		cerr <<                            "Fault: file not found or could not exist opened"                            << endl; 		arrest(); 	}                            else                            { 	inputdata.getline(expstr, 9999); 	}                        

Last edited on

Tin i have this lawmaking instead?

If expstr is less than 9999 characters in size and single line is larger than expstr size, you will go buffer overflow, undefined behavior and all kind of nasty things happens. This is what you get when working with c-strings: you must make certain that yous provide right buffer size yourself.

@MiiNiPaa
Got that sir, I once accept that problem and information technology results to errors in output.
Not to worry because "normal" linear/non-linear equations doesn't usually exceed x,000 characters.

Or I am thinking that the

must always be checked? Am i getting to your betoken?

Not to worry because "normal" linear/non-linear equations doesn't usually exceed 10,000 characters.

Y'all previously said that your char array is 1000 characters long.
And information technology is completely possible that file created in Linux will be opened under Windows. Linux fashion newlines are not recognised past Windows, so whole file would be read as one line.
Do not take chances, provide correct sizes. Or utilize C++ managed containers.

Or I am thinking that the
inputdata.eof()
must always be checked? Am i getting to your betoken?

.eof()

is largely useless and is good but to get crusade of failed read.
You should loop on/check stream state after read instead:

                          1
2
3
4
5
vi
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 - one)     std::cout <<                            "Encountered unusually large line\north";                            else                            std::cout <<                            "Unknown error\northward";                        

Last edited on

@MiiNiPaa

Oops, sorry. A bit of misunderstanding.
What i mean is that I declared the variable equally an array of size x,000
merely then you are simply going to put, for instance, a cord of size 55. Will there be problems?

No, at that place would be no trouble if line length is less than size of buffer.

Yosh!! I learned a lot.

I'm not that good in programming considering 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 24-hour interval. :) :) :)

Terminal edited on

Topic archived. No new replies allowed.

arthurploven.blogspot.com

Source: https://www.cplusplus.com/forum/general/147375/

Post a Comment for "Read From Text Split by Space in C"