I'm a complete n00b but how do you write some text to a file?
I wanna make a small program that writes a string and then read it.
How do i start?
I'm a complete n00b but how do you write some text to a file?
I wanna make a small program that writes a string and then read it.
How do i start?
Here's how to write to a text file
Run the above code and see what's being stored in the fileCode:#include <stdio.h> #include <conio.h> main() { FILE *fp; char ch, filename[40], mode[4]; while(1) { printf("Enter a filename: "); gets(filename); printf("\nEnter a mode (max. 3 characters): "); gets(mode); fp = fopen(filename, mode); if ( fp != NULL) { printf("\nSuccessful opening %s in mode %s.\n", filename, mode); fprintf(fp, "This will be written in the text file because it is under fprintf. The rest of the code is just to let you know how this works\n"); fclose(fp); puts("Enter 'x' to exit, any other to continue."); if ( (ch = getch() ) == 'x') { break; } else { continue; } } else { printf(stderr, "\nError opening file %s in mode %s.\n", filename, mode); puts("Enter 'x' to exit, any other to try again."); if ( (ch = getch()) == 'x') { break; } else { continue; } } } getchar(); getchar(); }
and why. If you observe the code carefully, you'll also see the syntax of fprintf. Apart from that, you can also learn a little bit about getch() and how that works.
Good Luck!
COOL PROGRAMS @ www.akilla.tk
Sorry it doesn't save a file...
And what's that mode-stuff?
This will probably look a little easier to you:
Code:#include <fstream> using std::ofstream; using std::endl; int main() { ofstream output("FileName.Txt"); output << "This text data will show up in the file!" << endl; return 0; }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
It works !! thanks man!!
But how do you load that file again in the program so it is viewable within the program?
For reading the data back into the program, something similar to this can be implemented (there are dozens of ways to do this):
Tested it using the file output by the first program and it works fine using my machine!Code:#include <fstream> #include <iostream> #include <string> using std::ifstream; using std::cout; using std::endl; using std::string; int main() { ifstream input("FileName.Txt"); string word; // Test to make sure the file is open, exit program if not open. if( !input.is_open() ) return 1; // File is open, read words in file one at a time and output to screen. input >> word; // Get first word while( input ) { cout << word << ' '; // Output words separated by spaces input >> word; // Get next word } cout << word << endl; // Output last word from file to screen return 0; }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
you can open the tutorial at cprogramming.com here
C++ Makes you Feel Better
"Gravity connot be held reponsible for people falling in love"--Albert Einstein
Tryt looking at the tutorial at gametutorials.com...
.