"fin." usage

This is a discussion on "fin." usage within the C++ Programming forums, part of the General Programming Boards category; Ok...all I want to do is write a "sentence" to a text file in plain ASCII format. "string" stops at ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    6

    Question "fin." usage



    Ok...all I want to do is write a "sentence" to a text file in plain ASCII format.

    "string" stops at a space
    "char" stops after a letter.

    Here is my source code, all I need help with is using the "fin."

    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>
    
    using namespace std;
    
    int main()
    {
        ifstream fin("file1.txt");
        char message[512];
        cout << "Enter a message:" << endl;
        cin >> message ;
        fin.open("file1.txt",ios::app);
        fin.getline(message, 512);
        ofstream SaveFile("file1.txt",ios::app);
    
        SaveFile << message;
        SaveFile << ".\n" ;
        SaveFile.close();
        return 0;
    }
    Thanks....I'm new to this stuff and doing is learning for me.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Use getline().
    Code:
        string message;
        cout << "Enter a message:" << endl;
        getline(cin, message);

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>

    These are the older headers, use the new ones:
    Code:
    #include <fstream>
    #include <iostream>
    #include <cstring>  // Or <string>?  Either way, you don't look like you really need this one
    Code:
    using namespace std;
    
    int main()
    {
        ifstream fin("file1.txt");
        char message[512];
        cout << "Enter a message:" << endl;
        cin >> message ;
        fin.open("file1.txt",ios::app);
    Your code is trying to open a file twice, the first line after the '{' opens a file called file1.txt for input. The fin.open() command then tries again to open the same file in append mode. Of course you should only need to open the file for input once. Also, append mode is only for output files, not input files.


    Code:
        fin.getline(message, 512);
    This is going to overwrite whatever the user typed into the buffer in response to the above prompt to enter a message. Are you simply thowing that input away in favor of getting a line from the input file or do you need to do something with that input before you get the line from the file?

    Code:
        ofstream SaveFile("file1.txt",ios::app);
    
        SaveFile << message;
        SaveFile << ".\n" ;
        SaveFile.close();
        return 0;
    }
    You should probably close the input file fin before you open another file object referencing the same file. The declaration of the SaveFile object needs to do a bitwise OR with ios::out, i.e. ofstream SaveFile("file1.txt",ios::out|ios::app);

    What sentence are you trying to write (append?) to the file? The one you appear to be reading from the input file, or the one you get from the user at the prompt?
    Last edited by hk_mp5kpdw; 02-22-2005 at 01:49 PM. Reason: bitwise OR, not bitwise AND
    "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

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    6

    Re:

    All I need it to do is save a sentence or even "blocks" of text until the user presses enter. I know how (I think, haha) to make it loop if I want to let the user enter multiple blocks of text, but for right now I just need it to save an entire block of text, spaces included.

    Its saying "SaveFile" is an undeclared funtion. What header does that use? I'm using the (new) headers you gave me.

    Code:
    #include <fstream>
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    	string message;
        ifstream fin("file1.txt");
        fin.open("file1.txt",ios::app);
        //ofstream SaveFile("file1.txt",ios::app);
        cout << "Enter a message:" << endl;
        getline(cin, message);
    	SaveFile << message;
    	SaveFile << ".\n" ;
        SaveFile.close();
    	return 0;
    }

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    1. Using a string object would require the <string> header instead of the <cstring> header.

    2. You are still attempting to open the input file twice.

    3. You are still trying to use append mode on an input file.

    4. Why do you even need an input file if you aren't using it for anything?

    Quote Originally Posted by osiris_99
    Its saying "SaveFile" is an undeclared funtion.
    Because you have commented out the line which declares the SaveFile object, i.e.:

    Code:
    //ofstream SaveFile("file1.txt",ios::app);
    You are attempting to use the SaveFile object in the lines below this point and it hasn't been declared anywhere because you have it commented out... that is why you get that message.

    If you simply want to get a message from the user, and append that to a file:

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <cstdlib> // For EXIT_FAILURE, EXIT_SUCCESS
    
    using namespace std;
    
    int main()
    {
        string message;
        ofstream SaveFile("file1.txt",ios::out|ios::app);
    
        // Make sure file is open, if not exit program
        if(!SaveFile.is_open())
        {
            cout << "Error attempting to open output file." << endl;
            return EXIT_FAILURE;
        }
    
        // Get message from user
        cout << "Enter a message: ";
        getline(cin,message);
    
        // Write/append message to end of file
        SaveFile << message << endl;
    
        return EXIT_SUCCESS;
    
    }
    "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

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    thank you...I got it now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 03:13 PM
  2. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 08:59 AM
  3. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 06:45 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 10:38 AM
  5. Win2K Limiting CPU Usage?
    By drdroid in forum Tech Board
    Replies: 4
    Last Post: 03-31-2004, 02:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21