File I/O not working correctly

This is a discussion on File I/O not working correctly within the C++ Programming forums, part of the General Programming Boards category; my WriteEntries function is not working... this snippet of code creates the file properly, but when i want to write ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    76

    File I/O not working correctly

    my WriteEntries function is not working... this snippet of code creates the file properly, but when i want to write the strings to the file... it does not work... anyone know why?

    Code:
    /*********************/
    /**** MCreateFile ****/
    /*********************/
    void MCreateFile()
    { 
        Phonebook pbEntry; 
             
        string filename;
        string directory = "Data/";
        string dirfile;
        
        CLEARSCREEN;
        
        cout<<"\n\t\t\tPlease enter the filename: ";
        cin>>filename;
        filename.append(".txt");
        
        dirfile=directory+filename;
        
        ifstream testFile;
        testFile.open(dirfile.c_str(), ios::in);
        
        fstream File(dirfile.c_str(), ios::out);
        
        if(testFile.good())
        {
           testFile.close();
           
           int samefile;
           
           cout<<"\n     "<<setfill('=')<<setw(70)<<"=";
           cout<<"\n     | There is a file at the current save location with the same name... |";
           cout<<"\n     "<<setfill('=')<<setw(70)<<"="<<endl;
           cout<<"\n\t\t\t  <1>Overwrite";
           cout<<"\n\t\t\t  <2>Rename Current File";
           cout<<"\n\t\t\t  <3>Quit";         
           cout<<"\n\t\t\t  ->: ";        
           cin>>samefile;
           switch(samefile)
           {
                           case 1:
                                File.open(dirfile.c_str(), ios::out|ios::trunc);
                                File.close();
                                break;
                           case 2:
                                filename.clear();
                                cout<<"\n\t\t\tNew File Name: ";
                                cin>>filename;
                                filename.append(".txt");
                                break;
                           case 3:
                                return;
                                break;
                           default:
                                   cout<<"\n\t\t\tNot a valid entry!";
                                   cout<<"\n\t\t\t  ->: ";
           }
        }
        
        dirfile=directory+filename;
        File.open(dirfile.c_str(), ios::out);      
        if(File.bad())
        {
                    cout<<"\t\t\tFile could not be created!";
                    return;
        }
        else
        {
            cout<<"\n\t\t\t  \'"<<filename<<"\' was created!!"<<endl<<endl;
            cin.ignore();
            cin.get();
            CLEARSCREEN;
        }
        
        int entrymenu;
        bool done=false;
        
        pbEntry.GetEntries(File);
        
        while(done==false)
        {
        cout<<setfill('-')<<setw(80)<<"-";
        cout<<"\t\t\t\t    "<<filename<<endl;
        cout<<setfill('-')<<setw(80)<<"-";
        
        pbEntry.DisplayEntries();
        
        cout<<"\t\t\t\t-(1)Add Entry"<<endl;
        cout<<"\t\t\t\t-(2)Delete Entry"<<endl;
        cout<<"\t\t\t\t-(3)Edit Entry"<<endl;
        cout<<"\t\t\t\t-(4)Save"<<endl;
        cout<<"\t\t\t\t-(5)Quit"<<endl;
        cout<<"\t\t\t\t->: ";
        cin>>entrymenu;
        switch(entrymenu)
        {
                         case 1:
                              pbEntry.Add();
                              break;
                         case 2:
                              pbEntry.Delete();
                              break;
                         case 3:
                              pbEntry.Edit();
                              break;
                         case 4:
                              pbEntry.WriteEntries(File);
                              CLEARSCREEN;
                              break;
                         case 5:
                              done=true;
                              File.close();
                              return;
                              break;
                         default:
                                cout<<"\t\t\tNot Valid!";
                                CLEARSCREEN;
        }
        }
    }
    Code:
    /*********************************/
    /**** Phonebook::WriteEntries ****/
    /*********************************/
    void Phonebook::WriteEntries(fstream &fFile)
    {
              cout<<"DEBUG2";   // Seeing if the program ever makes it to this function... which it does...
             for(unsigned int i=0;i<vEntries.size();i++)
             {
                     fFile<<"\n\t\t\tName\t\t\t:"<<vEntries[i].name;
                     fFile<<"\n\t\t\tCell Phone Number\t:"<<vEntries[i].cnumber;
                     fFile<<"\n\t\t\tHome Phone Number\t:"<<vEntries[i].hnumber;
                     fFile<<"\n\t\t\tAddress\t\t:"<<vEntries[i].address<<endl<<endl;
             }
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I would avoid associating File with a file until I was ready to use File as I'm not sure if a file can be associated with more than one stream at a time.

    If that didn't fix the problem I would want to know what happens to File in

    pbEntry.GetEntries(File);

    In particular, if File is closed/dissociated from the file in

    pbEntry.GetEntries(File);

    then it won't be available for use in

    pbEntry.WriteEntries(File);

    without reassociation/reopening the file.
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    If you want to use two different fstream objects, then use ifstream for the input stream and ofstream for the output stream. Otherwise you can do both input and output with just one fstream object.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    I would avoid associating File with a file until I was ready to use File as I'm not sure if a file can be associated with more than one stream at a time.
    Yea, I had it like this before... where the initial file isn't used till later...I'll change it back.

    Code:
    /*******************************/
    /**** Phonebook::GetEntries ****/
    /*******************************/
    void Phonebook::GetEntries(fstream &fFile)
    {
         string heading;
         Entry temp;
         
         if(fFile.peek()=='\0')
         {
                        return;
         }
         else
         {
             while(getline(fFile,heading,':'))
             {
                       getline(fFile,temp.name);
                       
                       getline(fFile,heading,':');
                       getline(fFile,temp.cnumber);
                       
                       getline(fFile,heading,':');
                       getline(fFile,temp.hnumber);
                       
                       getline(fFile,heading,':');
                       getline(fFile,temp.address);
                       
                       vEntries.push_back(temp);
             }                  
          }
    }
    Notice: This file input works without flaws...

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    I FOUND MY PROBLEM!!! Woot....
    i commented out the GetEntries... and my debugging labels worked...

    Code:
    /*********************/
    /**** MCreateFile ****/
    /*********************/
    void MCreateFile()
    { 
        Phonebook pbEntry; 
             
        string filename;
        string directory = "Data/";
        string dirfile;
        
        CLEARSCREEN;
        
        cout<<"\n\t\t\tPlease enter the filename: ";
        cin>>filename;
        filename.append(".txt");
        
        dirfile=directory+filename;
        
        ifstream testFile;
        testFile.open(dirfile.c_str(), ios_base::in);
        
        fstream File("hello.txt", ios_base::out);
        File<<"debug1 ";                         // DEBUG 1
        
        ofstream exFile("debug.txt", ios_base::out);
        
        if(testFile.good())
        {
           testFile.close();
           
           int samefile;
           
           cout<<"\n     "<<setfill('=')<<setw(70)<<"=";
           cout<<"\n     | There is a file at the current save location with the same name... |";
           cout<<"\n     "<<setfill('=')<<setw(70)<<"="<<endl;
           cout<<"\n\t\t\t  <1>Overwrite";
           cout<<"\n\t\t\t  <2>Rename Current File";
           cout<<"\n\t\t\t  <3>Quit";         
           cout<<"\n\t\t\t  ->: ";        
           cin>>samefile;
           switch(samefile)
           {
                           case 1:
                                //File.open(dirfile.c_str(), ios::out|ios::trunc);
                                //File.close();
                                break;
                           case 2:
                                filename.clear();
                                cout<<"\n\t\t\tNew File Name: ";
                                cin>>filename;
                                filename.append(".txt");
                                break;
                           case 3:
                                return;
                                break;
                           default:
                                   cout<<"\n\t\t\tNot a valid entry!";
                                   cout<<"\n\t\t\t  ->: ";
           }
        }
        
        dirfile=directory+filename;
        //File.open("hello.txt", ios_base::out);      
        if(File.bad())
        {
                    cout<<"\t\t\tFile could not be created!";
                    return;
        }
        else
        {
            cout<<"\n\t\t\t  \'"<<filename<<"\' was created!!"<<endl<<endl;
            cin.ignore();
            cin.get();
            CLEARSCREEN;
        }
        
        int entrymenu;
        bool done=false;
        
        File<<"debug2 ";                        // DEBUG 2
        
        //pbEntry.GetEntries(File);
        
        File<<"debug3 ";                        // DEBUG 3
        
        while(done==false)
        {
          cout<<setfill('-')<<setw(80)<<"-";
          cout<<"\t\t\t\t    "<<filename<<endl;
          cout<<setfill('-')<<setw(80)<<"-";
        
          File<<"debug4 ";                      // DEBUG 4
        
          pbEntry.DisplayEntries();
        
          File<<"debug5 ";                      // DEBUG 5
        
          cout<<"\t\t\t\t-(1)Add Entry"<<endl;
          cout<<"\t\t\t\t-(2)Delete Entry"<<endl;
          cout<<"\t\t\t\t-(3)Edit Entry"<<endl;
          cout<<"\t\t\t\t-(4)Save"<<endl;
          cout<<"\t\t\t\t-(5)Quit"<<endl;
          cout<<"\t\t\t\t->: ";
          cin>>entrymenu;
          switch(entrymenu)
          {
                           case 1:
                                pbEntry.Add();
                                break;
                           case 2:
                                pbEntry.Delete();
                                break;
                           case 3:
                                pbEntry.Edit();
                                break;
                           case 4:
                                pbEntry.WriteEntries(File);
                                CLEARSCREEN;
                                break;
                           case 5:
                                File<<"debug6 ";             // DEBUG 6
                                done=true;
                                File.close();
                                return;
                                break;
                           default:
                                  cout<<"\t\t\tNot Valid!";
                                  CLEARSCREEN;
          } 
        }
    }
    I figured out what was happening... I was using the File to input without ever changing the fstream openmode to ios_base::in instead of ios_base :: out!!

    Here is the text in my file debug.txt...

    Code:
    debug1 debug2 debug3 debug4 debug5 debug6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM

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