Open last used file

This is a discussion on Open last used file within the C++ Programming forums, part of the General Programming Boards category; I want to open the last used file i have my prog save the name of it in a text ...

  1. #1
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106

    Open last used file

    I want to open the last used file i have my prog save the name of it in a text file and then read the name of it but it wont open it.Please could someone ave a look and tell me whats the matter:

    Code:
    ifstream a_file("D:\\Text LOG\\Txt_log.txt");
                      getline(cin,txt);
                      a_file.close();
                      ifstream b_file(txt.c_str(),ios::app);
                      getline(b_file,txtt);
                      cout<< txtt;
                      cout<<setfill('-')<<setw(80)<<("-")<<endl;
                      cout<<"Please enter some text to be output to this file: ";
                      getline(cin,x);
                      b_file>> x;
                      cin.get();
                      system ("cls");
                      break;

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>ifstream b_file(txt.c_str(),ios::app);

    std::ios::app should be used for ofstreams when appending data to a file.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    I tried that still doesn't work.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Tried what exactly?

    Have you checked that the string you read from the first file is valid?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    ifstream a_file("D:\\Text LOG\\Txt_log.txt");
    getline(cin,txt);
    a_file.close();
    Try changing getline(cin,txt); to getline(a_file,txt).
    From the code you have right now, you are gathering input from the user through the cin stream and setting it to the string 'txt'.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    First, you read from b_file, then you try to write to it. Neither ifstream or ofstream allows this. Use fstream instead.

    Also, you do not do any error-checking to ensure the files have been opened correctly. You can use the methods is_open() and good() for this.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    New problem! I am starting from scratch and i am now adding a check to see if the file exists and well you can read the code and find out everything else.Please tell me whats the matter:
    Code:
    out<<"Selection 1. Create a new file and input some text."<<endl;
                                                   cout<<endl;
                                                   cout<<setfill('-')<<setw(80)<<"-"<<endl;
                                                   cout<<"Please enter the directory and filename:";
                                                   getline(cin,filename);
                                                   ofstream a_file(filename.c_str());
                                                   if (!a_file.is_open())
                                                   {
                                                   cout<<"Please input some text that will be written to the file you specified."<<endl;
                                                   cout<<">";
                                                   getline(cin,text);
                                                   cin.ignore();
                                                   a_file<< text;
                                                   a_file.close();
                                                   ofstream b_file("D:\LW_INFO\LUF.txt");
                                                   b_file<< filename;
                                                   b_file.close();
                                                   break;
                                                   }
                                                   else 
                                                   {
                                                        system("cls");
                                                        cout<<"Error: File already exists please specifiy a different name.";
                                                        cin.get();
                                                        system("cls");
                                                        break;
                                                   }

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you really want an answer you should not let us guess what the problem is.
    You're not telling what the problem is.
    I'm not telling what's wrong with that line
    Code:
    ofstream b_file("D:\LW_INFO\LUF.txt");
    Kurt

  9. #9
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    I changed that
    Code:
    ofstream b_file("D:\\LW_INFO\\LUF.txt");
    and sorry bout that. My problem is that the user enters a new filename and my prog makes it and my problem happens when it gets to this...
    Code:
    if (!a_file.is_open())
    I figuered out what it is. It makes the file THEN checks to see if the file exists and because they just made it it does. Please help(sorry i dont know how to word it,lol).

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    if you don't want to create a new file, you could at first open it for input. if that succeeds you could close it again and open it for output.
    Kurt

  11. #11
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    No, i want to create a new file.

  12. #12
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by L_U_K_E
    No, i want to create a new file.
    Then I don't understand the problem.
    EDIT: after thinking again.
    Still the same procedure to check if it exists open it for input. if that succeeds exit with an error message. If it doesn't open it for output.
    Kurt
    Last edited by ZuK; 06-05-2006 at 01:03 PM.

  13. #13
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    the user makes a NEW FILE, but i want my prog to check if a file in the SAME directory has the SAME filename if it does it displays an error message asking them to change the name if the name is unique it makes the NEW FILE and outputs text to that file.

  14. #14
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    See my edit
    Kurt

  15. #15
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Please could you look at the code and make some changes to it then post it here 4 me to see what it is thats the matter??
    Code:
    {
       system("cls");
       cout<<"Selection 1. Create a new file and input some text."<<endl;
       cout<<endl;
       cout<<setfill('-')<<setw(80)<<"-"<<endl;
       cout<<"Please enter the directory and filename:";
       getline(cin,filename);
       ofstream a_file(filename.c_str());
       if ( !a_file.is_open() )
       {
          a_file.close();
          ifstream b_file(filename.c_str());
          cout<<"Please input some text that will be written to the file you specified."<<endl;
          cout<<">";
          getline(cin,text);
          cin.ignore();
          cout<< text;
          b_file.close();
          ofstream c_file("D:\\W_INFO\\LUF.txt");
          c_file<< filename;
          c_file.close();
          break;
       }
       else
       {
          system("cls");
          cout<<"Error: File already exists please specifiy a different name.";
          cin.get();
          system("cls");
          break;
       }
    }
    Headers:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    #include <windows.h>
    Please if someone has the time look at this.
    Last edited by L_U_K_E; 06-06-2006 at 11:28 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM

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