PDA

View Full Version : c++ reading spaces in files



IanC
4th September 2004, 05:19 PM
(Using dev-cpp on win)

I have nearly finished an encryption program (bit of fun, got bored yesterday).
I can read all punctuation except a space.
I am using the following code to read the files:


ifstream cleart("encoded.txt");
for(int i=0;i<length;i++)
cleart>>cypherc[i];
(using the include fstream.h)
Where cypherc is a char array.
When the program reads in a space, it immediately skips and goes to the next character. Is there any way around this? It would be absurd if you could not read a space.


Ian

White_Hindu
4th September 2004, 05:42 PM
If there is an ASCII value for a space, then it might be easier...

Seraphim
4th September 2004, 07:06 PM
I believe the ASCII value for a space is 32. I could be wrong though so I'll check my table later.

Xv_raven_vX
4th September 2004, 07:16 PM
Is there a book or something that i can learn the basics of this? I'm hoping to become a computer programmer when i get out of highschool and i want have some knowledge of C++, php programming etc. So is there some place i can go to teach me the basics of it?

White_Hindu
4th September 2004, 10:36 PM
Try taking computer science in high school if you really want to learn. I can't think of an easier way.

IanC
5th September 2004, 05:59 AM
ASCII for a space is 32. I know that character variables can store spaces, as I have tried setting one and it worked.
Is it a particular property of the method I am using to read the file? If so, what other method would work for this?

Thanks,
Ian

dom
5th September 2004, 08:31 AM
its been a while [like 4 months] since ive programmed anything
but im pretty sure you need to use getline to read spaces.
the syntax is something like (using your existing code)


ifstream cleart("encoded.txt");
cleart.getline (cypherc, length);
or something like that.
okay i didnt use much of your existing code, but i thought i would.


just google for getline references to make sure the syntax is correct/works in your situation.


hope that helps

IanC
8th September 2004, 03:28 PM
Thanks Dom, works beautifully. Also means I can get rid of the for statements and it sorts out a problem I had with the end of a file.

Ian

MCeley
25th September 2004, 06:11 PM
Try taking computer science in high school if you really want to learn. I can't think of an easier way.

Computer Science in High School is now taught using Java instead of C++. I really wanted to learn it too but at least now I have a good understanding of Java.

ladymage
6th October 2004, 02:00 PM
the code should be something similar to
*remember to include the string library i.e. #include <string>
getline(cin, VarName); <-- THis is to get a line from the keyboard
getline(DataFileName, VarName); <-- To retrieve information from a text file,

That should do it.

Intel_Hydralisk
6th October 2004, 02:16 PM
Computer Science in High School is now taught using Java instead of C++. I really wanted to learn it too but at least now I have a good understanding of Java.

Java is a lot like C++ anyway... Taking computer science will strengthen you in basics of computer programming regardless of what language it's in... after learning something like Java, it'll be easy to pick up C++. I learned both (C++ sophomore year, and Java junior year)... I got a 5 on both APs... but the AP for the Java might as well have been in C++ as it barely had anything to do with Java.

MCeley
6th October 2004, 07:43 PM
I dont personally know C++ but there are two kids in my AB class that know it and they always get confused because they try and code things they way they are in C++ when using Java and just ends up not working out for them. Then again they arent the smartest kids in the world. I have a pretty good understanding of Java though since I took AP Computer Science A last year and im taking AB this year.