Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Craazer

Storing string to a file

This topic is 5683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can i store strings to a file? u see if i have: string name="McCall"; and i store it to file using ofstream, its just numbers in the file so obviously its kinda difficult to load from the file. Soo does the string type have any operator perhaps to get chars, not the memory address or what ever it gaved to me?

Share this post


Link to post
Share on other sites
Advertisement
Include your write/read code. Here that is.



I have spoken.


[edited by - Zorak on December 23, 2002 10:43:15 AM]

Share this post


Link to post
Share on other sites
Here is code that shows both how to read and write strings to a file:

#include <stdio.h>

int main()
{
FILE* fptr;
char mystr[] = "Hello World";
char anotherstr[101];

/* Write "Hello World" to file "hi.txt" */
fptr = fopen("hi.txt", "wt"); /* w means write, t means text file */
fprintf(fptr, "%s", mystr); /* Write string mystr */
fclose(fptr);

/* Now read that string back in */
fptr = fopen("hi.txt", "rt"); /* r means read */
fscanf(fptr, "%100s", anotherstr); /* Read string in */
fclose(fptr);

/* To prove this worked, we''ll print anotherstr to the screen */
printf("The string is: %s\n", anotherstr);

return 0;
}

- Andy Oxfeld

Share this post


Link to post
Share on other sites
quote:
Original post by Evil Bill
Unfortunately that uses C style strings, and the OP was using std::string (well ''string'', which is a class anyway)


Then it''s about time for the OP to learn to use C style strings

- Andy Oxfeld

Share this post


Link to post
Share on other sites
You could write a string similarly to my example above but using stringvariablename.c_str() where I used mystr, like this:

fprintf(fptr, "%s", stringvariablename.c_str());

To read in a string, you could read it into a character array like I did, and then do:

stringvariablename = anotherstr;

- Andy Oxfeld

Share this post


Link to post
Share on other sites
Uh, what''s wrong with ofstream/ifstream?

This should work (not tested)

  
#include <fstream>
#include <iostream>
#include <string>

int main() {
std::string st = "boo";
{
std::ofstream out("test.txt");
out << st;
}

std::string st2;
{
std::ifstream in("test.txt");
in >> st2;
}
std::cout << st2 << std::endl;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!