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:
| |
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
expstris of type character, all through out the program. So definitely, information technology will impale me if I take to change all to cord.
| |
Perhaps sharing the bodily type of
expstrwould 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?
| |
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?
is largely useless and is good but to get crusade of failed read.
You should loop on/check stream state after read instead:
| |
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.
Source: https://www.cplusplus.com/forum/general/147375/
Post a Comment for "Read From Text Split by Space in C"