Thread: Still can not write or read from save file

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    56

    Post Still can not write or read from save file

    I am wondering if it has anything to do with the program that saves the file. Here is the code for that program

    [CODE] #include<iostream>
    #include<iomanip>
    #include<string>
    #include<cstdlib>
    #include<ctime>
    #include<fstream>
    #include<algorithm>
    int getpin(int & );
    int i;



    using namespace std;

    int main()
    {

    //declare variables

    string clerkId ="";
    int ssn =0;
    string name ="";
    string address ="";
    string city ="";
    string state ="";
    int zip =0;
    int pin = 0;
    int visa = 0;

    //enter input itesm to a file

    ofstream outfile;
    outfile.open("WACKOFILE.dat", ios:ut);

    if(outfile.is_open())
    {

    //Clerk is to enter ID for login Information

    cout<<"Enter ClerkId: ";
    getline(cin, clerkId);

    //This will Loop Through Program and then Exit Loop
    //Once Clerk-ID id Null

    while(clerkId != "xx")
    {

    //Get Name Address City State Zip Social Security Number


    cout <<"Enter SSN: ";
    cin>>ssn;
    cin.ignore(1);
    cout <<"Enter Name: ";
    getline(cin, name);
    cout <<"Enter Address: ";
    getline(cin, address);
    cout <<"Enter City: ";
    getline(cin, city);
    cout <<"Enter State: ";
    getline(cin, state);
    cout <<"Enter Zip: ";
    cin>>zip;
    pin = getpin(visa);
    cin.ignore(1);
    cin.ignore(1);

    // Format Title Lines for Name, Address, City, State, and Zip

    cout<<setw(25)<<left<<"Name"<<" "<<setw(25)<<left<<"Address"<<" "
    <<setw(16)<<left<<"City"<<" "<<setw(2)<<"ST"<<" "<<setw(5)<<left<<"Zip"<<endl;


    //Format Display Output

    cout<<setw(25)<<left<<name<<" "<<setw(25)<<left<<address<<" "
    <<setw(16)<<left<<city<<" "<<setw(2)<<state<<" "<<setw(5)<<setfill('0')<<right<<zip<<endl;
    cout<<setw(2)<<left<<"ClerkID"<<" "<<setw(9)<<setfill(' ')<<left<<"SSN"<<endl;
    cout<<setw(2)<<left<<clerkId<<" "<<setw(9)<<left<<ssn<<endl;


    cout<<"Enter ClerkId: ";
    getline(cin, clerkId);


    }
    }
    else
    {cout<<"File not Open"<<endl;}
    outfile.close();
    return 0;

    }

    int getpin(int & visa)

    {



    srand(time(NULL));

    visa = 10000 + rand()%(99999-10000 + 1);
    return 10000 + rand()%(9999-1000 + 1);

    }

    [CODE]
    Any and all help please, I am going nuts with this one.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    might have to do with how you open the file. i don't know of the ios::ut flag, try something else. http://www.cplusplus.com/ref/iostream/fstream/open.html shows some flags you should be able to use.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well, you're outputting to the screen but not to the file. For example, to output to both:
    Code:
    //Format Display Output
    
    cout<<setw(25)<<left<<name<<" "<<setw(25)<<left<<address<<" "
    <<setw(16)<<left<<city<<" "<<setw(2)<<state<<" "<<setw(5)<<setfill('0')<<right<<zip<<endl;
    cout<<setw(2)<<left<<"ClerkID"<<" "<<setw(9)<<setfill(' ')<<left<<"SSN"<<endl;
    cout<<setw(2)<<left<<clerkId<<" "<<setw(9)<<left<<ssn<<endl;
    
    outfile<<setw(25)<<left<<name<<" "<<setw(25)<<left<<address<<" "
    <<setw(16)<<left<<city<<" "<<setw(2)<<state<<" "<<setw(5)<<setfill('0')<<right<<zip<<endl;
    outfile<<setw(2)<<left<<"ClerkID"<<" "<<setw(9)<<setfill(' ')<<left<<"SSN"<<endl;
    outfile<<setw(2)<<left<<clerkId<<" "<<setw(9)<<left<<ssn<<endl;

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    56
    So what your saying is that I need to put this in the code I wrote to save the file so that I will be able to read it when I open it with the other program. Am I right in the way I am looking at this?

    [CODE]

    outfile<<setw(25)<<left<<name<<" "<<setw(25)<<left<<address<<" "
    <<setw(16)<<left<<city<<" "<<setw(2)<<state<<" "<<setw(5)<<setfill('0')<<right<<zip<<endl;
    outfile<<setw(2)<<left<<"ClerkID"<<" "<<setw(9)<<setfill(' ')<<left<<"SSN"<<endl;
    outfile<<setw(2)<<left<<clerkId<<" "<<setw(9)<<left<<ssn<<endl;

    [CODE]

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >So what your saying is that I need to put this in the code I wrote to save the file
    That would be my guess.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    56
    Quote Originally Posted by swoopy
    >So what your saying is that I need to put this in the code I wrote to save the file
    That would be my guess.
    Still not working, I put it in both programs. It still does the same thing

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What's in WACKOFILE.dat? Have you looked at it with an editor to see what's in it?

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    56
    Quote Originally Posted by swoopy
    What's in WACKOFILE.dat? Have you looked at it with an editor to see what's in it?
    Yes it has this in it...
    [CODE]Joseph L. Gelsomino 123 Any Any Town RI 02852
    ClerkID SSN
    uu 90090909 [CODE\]

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Great, it looks like it's working.

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    56

    Post it does?

    Quote Originally Posted by swoopy
    Great, it looks like it's working.
    [QUOTE] SO what is it that I am doing wrong?[\QUOTE]

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If the Console Window is disappearing too soon, you might want to read this FAQ entry:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Otherwise give a detailed explanation of your problem, as it's not clear.

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    56
    Quote Originally Posted by swoopy
    If the Console Window is disappearing too soon, you might want to read this FAQ entry:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Otherwise give a detailed explanation of your problem, as it's not clear.
    When I run the second program, it opens the console window from the save file which has been made from the first program, but in the window it says "press any key to continue" now when i press a key the window shuts, so I can't write to the file or view the file. I wish to be able to write to the file and then have it read back to me.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If the second program is reading what the first program wrote, then you should check to be sure the file was opened:
    Code:
    	inFile.open("WACKOFILE.dat", ios::in);
    	if(!inFile.is_open())
    	{
    	   cout << "Unable to open file.  Press <Enter> to quit." << endl;
    	   cin.get();
    	   return 1;
    	}
    Then if the file opened correctly, add cout's after each read to be sure each piece of data is read properly.

  14. #14
    Registered User
    Join Date
    Oct 2004
    Posts
    56
    Quote Originally Posted by swoopy
    If the second program is reading what the first program wrote, then you should check to be sure the file was opened:
    Code:
    	inFile.open("WACKOFILE.dat", ios::in);
    	if(!inFile.is_open())
    	{
    	   cout << "Unable to open file.  Press <Enter> to quit." << endl;
    	   cin.get();
    	   return 1;
    	}
    Then if the file opened correctly, add cout's after each read to be sure each piece of data is read properly.
    I just try what you said, and sure enough the file does not open. Now I am really lost. What in the world do I do now?

  15. #15
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Where is your WACKOFILE.dat being stored?

    Try specifiying the whole path, i.e. "C:\MyFolder1\MyFolde2\InHere\WACKOFILE.dat"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Replies: 20
    Last Post: 07-24-2007, 11:47 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM