Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write std::string to file?

Tags:

c++

I want to write a std::string variable I am accepting from the user to a file. I tried using the write() method and it writes to the file. But when I open the file I see boxes instead of the string.

The string is only a variable length single word. Is std::string suitable for this or should I use a character array or something.

ofstream write; std::string studentName, roll, studentPassword, filename;   public:  void studentRegister() {     cout<<"Enter roll number"<<endl;     cin>>roll;     cout<<"Enter your name"<<endl;     cin>>studentName;     cout<<"Enter password"<<endl;     cin>>studentPassword;       filename = roll + ".txt";     write.open(filename.c_str(), ios::out | ios::binary);      write.put(ch);     write.seekp(3, ios::beg);      write.write((char *)&studentPassword, sizeof(std::string));     write.close();` } 
like image 553
Slashr Avatar asked Mar 13 '13 14:03

Slashr


People also ask

How do you write a string to a file C++?

Using the fwrite() Function to write String to File in C++ Therefore, you must open the file using the fopen() function that returns the FILE pointer. The fopen() function is similar to the open() function of the fstream class. It accepts the path of the file and the mode of opening the file and returns a FILE pointer.

What does std::string () do?

std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

Does std::string make a copy?

std::string_view provides read-only access to an existing string (a C-style string literal, a std::string , or a char array) without making a copy.

How do I write a string to a text file?

io 1 Create file object with the path to the text file. 2 Have your data ready in a string. 3 Call the method FileUtils.writeStringToFile () and pass the file object, data string as arguments to it. The function writes the data to file.

How to write a string to a file in Python?

Have your data ready in a string. Call the method FileUtils.writeStringToFile () and pass the file object, data string as arguments to it. The function writes the data to file. System.out.print ("Data written to file successfully."); Run this program, and you will get the following output.

How do I write a string to a file in Commons?

Example – Write String to File using commons.io 1 Create file object with the path to the text file. 2 Have your data ready in a string. 3 Call the method FileUtils.writeStringToFile () and pass the file object, data string as arguments to it. The function writes the data to file.

How to write to a file in Java?

Done Writing! Another method to write to file is the built-in write function that can be called from the fstream object. The write function takes a character string pointer and the size of the data stored at that address. As a result, given data is inserted into the file associated with the calling stream object.


2 Answers

You're currently writing the binary data in the string-object to your file. This binary data will probably only consist of a pointer to the actual data, and an integer representing the length of the string.

If you want to write to a text file, the best way to do this would probably be with an ofstream, an "out-file-stream". It behaves exactly like std::cout, but the output is written to a file.

The following example reads one string from stdin, and then writes this string to the file output.txt.

#include <fstream> #include <string> #include <iostream>  int main() {     std::string input;     std::cin >> input;     std::ofstream out("output.txt");     out << input;     out.close();     return 0; } 

Note that out.close() isn't strictly neccessary here: the deconstructor of ofstream can handle this for us as soon as out goes out of scope.

For more information, see the C++-reference: http://cplusplus.com/reference/fstream/ofstream/ofstream/

Now if you need to write to a file in binary form, you should do this using the actual data in the string. The easiest way to acquire this data would be using string::c_str(). So you could use:

write.write( studentPassword.c_str(), sizeof(char)*studentPassword.size() ); 
like image 101
JSQuareD Avatar answered Sep 16 '22 16:09

JSQuareD


Assuming you're using a std::ofstream to write to file, the following snippet will write a std::string to file in human readable form:

std::ofstream file("filename"); std::string my_string = "Hello text in file\n"; file << my_string; 
like image 33
stefan Avatar answered Sep 18 '22 16:09

stefan