Thread: Write some text to a file

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    7

    Question Write some text to a file

    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?

  2. #2
    Akilla
    Guest

    Here..

    Here's how to write to a text file
    Code:
    #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();
    
    }
    Run the above code and see what's being stored in the file
    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

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    7
    Sorry it doesn't save a file...
    And what's that mode-stuff?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    7
    It works !! thanks man!!
    But how do you load that file again in the program so it is viewable within the program?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    For reading the data back into the program, something similar to this can be implemented (there are dozens of ways to do this):
    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;
    }
    Tested it using the file output by the first program and it works fine using my machine!
    "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

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    106
    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

  8. #8
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605

    Write Text To File

    Tryt looking at the tutorial at gametutorials.com...
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. How to write data in exist text file?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2002, 05:55 AM