0

i have a little problem on writing the string into a file, How can i write the string into the file and able to view it as ascii text? because i am able to do that when i set the default value for str but not when i enter a str data Thanks.

#include <iostream> #include <fstream> #include <cstring> using namespace std; int main() { fstream out("G://Test.txt"); if(!out) { cout << "Cannot open output file.\n"; return 1; } char str[200]; cout << "Enter Customers data seperate by tab\n"; cin >> str; cin.ignore(); out.write(str, strlen(str)); out.seekp(0 ,ios::end); out.close(); return 0; } 
2
  • 1
    just as a sidenote. Having: char str[200]; and then reading text into it from the input is a bad idea. The moment someone types in more than 200 characters, your program will behave undefined and probably crash Commented Oct 21, 2009 at 17:38
  • std::string may not be well designed, but it's your friend. cin >> str will work just fine if str is a std::string. Commented Oct 23, 2009 at 19:53

4 Answers 4

8

Please use std::string:

#include <string> std::string str; std::getline(cin, str); cout << str; 

I'm not sure what the exact problem in your case was, but >> only reads up to the first separator (which is whitespace); getline will read the entire line.

Sign up to request clarification or add additional context in comments.

1 Comment

Definately worth noting that << and >> with strings behave differently in relation to White Space. +1
1

Just note that >> operator will read 1 word.

std::string word; std::cin >> word; // reads one space seporated word. // Ignores any initial space. Then read // into 'word' all character upto (but not including) // the first space character (the space is gone. // Note. Space => White Space (' ', '\t', '\v' etc...) 

Comments

1

You're working at the wrong level of abstraction. Also, there is no need to seekp to the end of the file before closing the file.

You want to read a string and write a string. As Pavel Minaev has said, this is directly supported via std::string and std::fstream:

#include <iostream> #include <fstream> #include <string> int main() { std::ofstream out("G:\\Test.txt"); if(!out) { std::cout << "Cannot open output file.\n"; return 1; } std::cout << "Enter Customer's data seperated by tab\n"; std::string buffer; std::getline(std::cin, buffer); out << buffer; return 0; } 

If you want to write C, use C. Otherwise, take advantage of the language you're using.

1 Comment

I'd use std::ofstream fro writing.
0

I can't believe no one found the problem. The problem was that you were using strlen on a string that wasn't terminated with a null character. strlen will keep iterating until it finds a zero-byte, and an incorrect string length might be returned (or the program might crash - it's Undefined Behavior, who knows?).

The answer is to zero-initialize your string:

char str[200] = {0}; 

Supplying your own string as the value of str works because those in-memory strings are null-terminated.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.