File output

This is a discussion on File output within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm trying to write a program that is supposed to be a tool of sorts for another program I'm ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    7

    File output

    Hello, I'm trying to write a program that is supposed to be a tool of sorts for another program I'm working on, and I am storing what I want to be written to the file in strings until the program gets to the writing portion. The IDE I'm using is Dev-C++ 4.9.2-ish, with g++.exe for my C++ compiler. There are several errors that are being shown between lines 49 and 58. The error on 49 is "no match for 'operator<<' in 'fichier << i_path' ", on 50" invalid operands of types `std:fstream ()(std::string)' and `int' to binary `operator<<' ", 51 "no match for 'operator<<' in 'fichier << keyword' ", 52 "no match for 'operator<<' in 'fichier << question' ", 55 "no match for 'operator<<' in 'fichier << choice[counter]' ", and with 58 " invalid operands of types `std:fstream ()(std::string)' and `int' to binary `operator<<' ". If someone can explain what these errors mean and/or how I can resolve them, please reply. Below is the program's code.

    Warning! This is code! (Bad MythBusters spoof, all well...):
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main(){
        cout<<"Welcome to the CHEM BOWL 2008 Question Maker program. Using this\nprogram, you will be able to make question files";
        cout<<"for use with the CHEM BOWL 2008 Review Program.\n";
        cout<<"To start, please enter the local path of an image you want to associate with this question.\n";
        cout<<"If you don't want to supply an image, type none.\n";
        cout<<"Example: Images\acids.bmp\n";
        string i_path;
        getline (cin, i_path, '\n');
        cout<<"Now, please enter the difficulty of the quetion, on an integer scale of between 1 and 10 inclusive.\n";
        int difficulty;
        cin>>difficulty;
        cout<<"Here, you can supply a keyword to find an appropriate review section with.\n";
        string keyword;
        getline (cin, keyword, '\n');
        cout<<"On this line, please type your question.\n";
        string question;
        getline (cin, question, '\n');
        cout<<"Here, type the first choice.\n";
        string choice[4];
        getline (cin, choice[0], '\n');
        cout<<"Here, type the second choice.\n";
        getline (cin, choice[1], '\n');
        cout<<"The third choice.\n";
        getline (cin, choice[2], '\n');
        cout<<"The fourth choice.\n";
        getline (cin, choice[3], '\n');
        cout<<"And finally, the fifth choice.\n";
        getline (cin, choice[4], '\n');
        cout<<"Here, type the number of the correct choice.\n";
        int correct_choice;
        cin>>correct_choice;
        cout<<"Give this file a number that is not already used by another question here.\n";
        string id;
        getline(cin, id, '\n');
        
        //This section writes the file.
        string file;
        string txt;
        txt=".txt";
        file=id+txt;
        ofstream fichier (string file);
        fichier<<i_path;
        fichier<<difficulty;
        fichier<<keyword;
        fichier<<question;
                        
        for (int counter=0;counter<=5;counter++){
            fichier<<choice[counter];
        }
                                         
        fichier<<correct_choice;
        cout<<"The file should be created, please check.";
        cin.get();
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > ofstream fichier (string file);
    This should be:
    Code:
        ofstream fichier (file.c_str());
    Or you can also do this:
    Code:
        ofstream fichier ((id+".txt").c_str());

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    You don't need to write a variable that holds .txt and the name of the file, if you create a file it will automatically make it a .txt file. You can also just use getline again so the user can input their own name of the file, with a file extension of their choice. I might have missed it, but where did you open the file, then check to make sure the file was opened, then closed again? Maybe something like this?

    Code:
        ofstream fichier;
        fichier.open(file);
        if(fichier.is_open()){
        	fichier << i_path;
        	fichier << difficulty;
        	fichier << keyword;
        	fichier << question;
    
        	for (int counter=0;counter<=5;counter++){
        	    fichier<<choice[counter];
        	}
    
        	fichier << correct_choice;
    	}
        fichier.close();
    EDIT: swoopy beat me to it.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    7

    It's almost done :)

    The program is almost working, thanks to you, scwizzo and swoopy, but there's one crucial problem: the program will make a the file, and give it a name, but its leaving the file empty. I'm going to try to work on this some more later, after getting some sleep, but here's the code thus far:
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main(){
        cout<<"Welcome to the CHEM BOWL 2008 Question Maker program. Using this\nprogram, you will be able to make question files";
        cout<<"for use with the CHEM BOWL 2008 Review Program.\n";
        cout<<"To start, please enter the local path of an image you want to associate with this question.\n";
        cout<<"If you don't want to supply an image, type none.\n";
        cout<<"Example: Images\\acids.bmp\n";
        string i_path;
        getline (cin, i_path, '\n');
        cout<<"Now, please enter the difficulty of the quetion, on an integer scale of between 1 and 10 inclusive.\n";
        int difficulty;
        cin>>difficulty;
        cin.get();
        cout<<"Here, you can supply a keyword to find an appropriate review section with.\n";
        string keyword;
        getline (cin, keyword, '\n');
        cin.get();
        
        cout<<"On this line, please type your question.\n";
        string question;
        getline (cin, question, '\n');
        cin.get();
        
        cout<<"Here, type the first choice.\n";
        string choice[4];
        getline (cin, choice[0], '\n');
        cin.get();
        
        cout<<"Here, type the second choice.\n";
        getline (cin, choice[1], '\n');
        cin.get();
        
        cout<<"The third choice.\n";
        getline (cin, choice[2], '\n');
        cin.get();
        
        cout<<"The fourth choice.\n";
        getline (cin, choice[3], '\n');
        cin.get();
        
        cout<<"And finally, the fifth choice.\n";
        getline (cin, choice[4], '\n');
        cin.get();
        
        cout<<"Here, type the number of the correct choice.\n";
        int correct_choice;
        cin>>correct_choice;
        cin.get();
        
        cout<<"Give this file a number that is not already used by another question here.\n";
        string id;
        getline(cin, id, '\n');
        cin.get();
        
        //This section writes the file.
        string file;
        file=id+".txt";
        ofstream fichier (file.c_str());
        fichier.open(file.c_str());
        if(fichier.is_open()){
        	fichier << i_path;
        	fichier << difficulty;
        	fichier << keyword;
        	fichier << question;
    
        	for (int counter=0;counter<=5;counter++){
        	    fichier<<choice[counter];
        	}
    
        	fichier << correct_choice;
    	}
        fichier.close();
        cout<<"The file should be created, please check.";
        cin.get();
        cin.get();
    }
    //Credit to scwizzo and swoopy of the Cprogramming boards to getting this
    //program to write files.
    EDIT:
    Thanks again, you two.
    Last edited by NESevolved; 08-29-2007 at 12:44 AM. Reason: P.S.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> string choice[4];
    That code declares an array of size 4. That means that only the indexes 0, 1, 2 and 3 are valid. You try to add 5 strings to this array, and so you will get undefined behavior which could include a crash. If you want to store five choices, declare the array to have 5 strings.

    >> ofstream fichier (file.c_str());
    This automatically opens the file.

    >> fichier.open(file.c_str());
    This tries to open it again. It is not necessary and might be causing you a problem. Remove it.

    >> for (int counter=0;counter<=5;counter++){
    That loop runs six times. The indexes it loops through are 0, 1, 2, 3, 4 and 5. Your array only has a size of 4 as I mentioned above. Even if you change it to an array of 5 strings, this loop needs to be updated to only run when counter is less than 5 but not equal.

    >> fichier<<choice[counter];
    You are outputting the strings but are not outputting any separator. The strings will all run together. Make sure to add a newline or space or comma or something so you can find the separation of the strings.

    Finally. you are calling cin.get() after your calls to getline. Are you doing this on purpose to force the user to hit enter twice? If not, then remove them. You only need the extra cin.get() after calls to cin >>.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    For the most part it is working if you do it this way

    Code:
        //This section writes the file.
        string file;
        file=id+".txt";
        ofstream fichier; //you had (file.c_str()) here, which didnt go there
        fichier.open(file.c_str());
        if(fichier.is_open()){
        	fichier <<"file: "<< i_path << endl;
        	fichier <<"diff: "<< difficulty << endl;
        	fichier <<"keyw: "<< keyword << endl;
        	fichier <<"ques: "<< question << endl;
        	fichier <<"corr: "<< correct_choice << endl;
    
        	for (int counter=0;counter<=4;counter++){
        	    fichier<<"choice "<<counter<<": "<<choice[counter] << endl;
        	}
        }
        fichier.close();
        cout<<"The file should be created, please check.";
        cin.get();
        cin.get();
    }
    There's just a problem with the way it reads in the question. The question comes up as whatever is entered in the fifth choice variable. Other than that, it will create, and write to the file now.

    EDIT: I was beat again... but yea what daved just said.
    Last edited by scwizzo; 08-29-2007 at 10:40 AM.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    7

    Smile It works.

    Alright it works now, thanks. Its been a while since I've done some programming, so thanks for reminding me of some of the basics (declaring an array uses the number of entries) and helping me to learn the basics of file I/O.
    EDIT:
    Here's the code for the final, working version, and thanks again.

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main(){
        cout<<"Welcome to the CHEM BOWL 2008 Question Maker program. Using this\nprogram, you will be able to make question files";
        cout<<"for use with\nthe CHEM BOWL 2008 Review Program.\n";
        cin.get();
        cout<<"To start, please enter the local path of an image you want to\nassociate with this question.\n";
        cout<<"If you don't want to supply an image, type none.\n";
        cout<<"Example: Images\\acids.bmp\n";
        string i_path;
        getline (cin, i_path);
       
        cout<<"\nNow, please enter the difficulty of the quetion, on a scale of\nbetween 1 and 10 inclusive.\n";
        float difficulty;
        cin>>difficulty;
        cin.get();
        
        cout<<"\nHere, you can supply a keyword to find an appropriate review\nsection with.\n";
        string keyword;
        getline (cin, keyword);
        
        cout<<"\nOn this line, please type your question.\n";
        string question;
        getline (cin, question);
        
        cout<<"\nHere, type the first choice.\n";
        string choice[5]; //Future note: actual number of entries [5].
        getline (cin, choice[0]);
        
        cout<<"\nHere, type the second choice.\n";
        getline (cin, choice[1]);
        
        cout<<"\nThe third choice.\n";
        getline (cin, choice[2]);
        
        cout<<"\nThe fourth choice.\n";
        getline (cin, choice[3]);
        
        cout<<"\nAnd finally, the fifth choice.\n";
        getline (cin, choice[4]);
        
        cout<<"\nHere, type the number of the correct choice.\n";
        int correct_choice;
        cin>>correct_choice;
        cin.get();
        
        cout<<"\nGive this file a number that is not already used by another\nquestion here.\n";
        string id;
        getline(cin, id);
        
        //This section writes the file.
        string file;
        file="Questions\\"+id+".txt";
        ofstream fichier;
        fichier.open(file.c_str());
        
        if(fichier.is_open()){
        	fichier<<i_path;
        	fichier<<"\n";
        	fichier<<difficulty;
        	fichier<<"\n";
        	fichier<<keyword;
        	fichier<<"\n";
        	fichier<<question;
            fichier<<"\n";
        	for (int counter=0;counter<5;counter++){
        	    fichier<<choice[counter];
        	    fichier<<"\n";
        	}
        	fichier << correct_choice;
    	}
        fichier.close();
        cout<<"\nCongrats! Your new question file is complete.\n";
        cout<<"Would you like to restart the program?\n1. Yes\n2. No\n\n";
        int ans;
        cin>>ans;
        switch (ans){
               case 1:
                    cout<<"\n\n\nRestarting.";
                    main();
                    break;
               case 2:
                    cout<<"\nThanks to:\nDaved, scwizzo, and swoopy of the C-Programming Boards for their\nhelp with the file I/O system.";
                    break;
               default:
                       cout<<"Bad input. Now terminating.";
                       break;
                       }
        cin.get();
        cin.get();
    }
    Last edited by NESevolved; 08-29-2007 at 12:54 PM. Reason: Wanted to add code for the final version.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

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