Help with chars

Hello,

i'm trying to make a text file based program and trying to store my data into files. While in progress i noticed that i can't store a sentence in my file, help?

char word[100];

My sentence: dog is furry

But i noticed that char don't store space's and just creates new files...

As i know string stores 1 word and char can store a lot with spaces dashes commas etc?

Am i wrong?

Cheers.
Am i wrong?

Yes. C strings (a.k.a. char arrays) can store everything except null bytes, strings can store everything.
You likely read the sentence incorrectly. You should use getline if you want to include whitespaces, e.g.:
1
2
string line;
getline(cin,line);
Thanks for reply!

But getline is used in reading file, and im writing in file.
You can write a string to a stream using operator<<, whether it contains spaces or not.
im using this code...
1
2
3
4
5
6
7
8
char wordContent[100];

cin >> wordContent;

ofstream addC;
addC.open(fileName.c_str());
addC << wordContent;
addC.close();


You can write a string to a stream using operator<<, whether it contains spaces or not.


Can you give a little example? i don't understand english smart way.
Never mind that. Like I said, you're reading the string incorrectly - you're reading only a word instead of a sentence.
Use getline.
getline(addC, wordContent);

no matching function for call to `getline(std::ofstream&, std::string&)'


hm?
Did you include <string>?
Edit: you're supposed to read from cin, not addC. You can't read from an output stream.
Last edited on
cin.getline(wordContent,100);

doesnt write to file, can you just write me a correct line? would appreciate it.

edit: and dont work like cin command.
Last edited on
1
2
string wordContent;
getline(cin,wordContent);
Last edited on
solved
Last edited on
Topic archived. No new replies allowed.