Search for .txt file

This is a discussion on Search for .txt file within the C++ Programming forums, part of the General Programming Boards category; I would like to know if there is any way that the user could enter a filename and have my ...

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

    Search for .txt file

    I would like to know if there is any way that the user could enter a filename and have my program open the file.The thing is all of the files already exist and i want my prog to search for the file and then open it.

    I was thinking maybe something like this:
    Code:
    case 1:
    {
       cout<<"enter filename:";
       getline(cin,filename);
       ofstream a_file("Ingrediants\\"<<filename<<".txt";
       a_file>> information;
       a_file.close();
       cout<<information;
    }
    But im not sure this will work because of the:
    Code:
       ofstream a_file("Ingrediants\\"<<filename<<".txt";
    part of the file.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    I think it should be:
    Code:
    ofstream a_file(("Ingrediants\\" + filename + ".txt").c_str());
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Code:
    getline(cin,filename);
    filename = "Ingrediants\\" + filename + ".txt";
    a_file.open(filename.c_str());
    EDIT: Though perhaps laserlight's suggestion would be better assuming you want to maintain the integrity of the original file name.
    Sent from my iPadŽ

  4. #4
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Thanx for both of your help ill try them when i get home(lol)!

  5. #5
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Right i can now open the file but why wont this piece of code work:
    Code:
    getline(file1,info1);
    this works
    Code:
    getline(cin,info1);
    but this accepts user input but i want to read in the first line of the file and then do whatever with it(lol).

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    The << operator is only overloaded (overloaded == used for special things) for stream operations like cout and cin (and file objects you create). It's not a magic C++ function.

    By the way, your line is missing a ")" in the end.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Could be some problem with file1. How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    well afterwards i have the line:
    Code:
    cout<<info1;
    however nothing is output onto the screen(??).

  9. #9
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    by the way i get the error message :

    "no matching function to call to 'getline(std:fstream&,std::string&)'

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    getline() reads from an input stream. file1 is an output stream.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    i get ya (i hope), so i just need to open the file for reading from then im sorted.But how do i do that??

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    so i just need to open the file for reading from then im sorted
    Use std::ifstream?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Have you made sure that your file was opened correctly?

    Code:
    ifstream file("foobar.txt");
    if(!file.is_open() || file.bad())
    {
        cout << "Unable to open file!\n";
    }
    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;}

  14. #14
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Yeah i thought of that but im fairly new so well yeah!I'll try it.

  15. #15
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    yeah i ave checked to see if the file is open!

    but i ave aother problem (sorry about this i said i was new to this)
    Code:
                                          getline(file2,info);
                                          cout<<"Name:" + info << endl;
                                          getline(file2,info2);
                                          cout<<"Value:" + info2 << endl;
                                          getline(file2,info3);
                                          cout<<"Weight:" + info3 << endl;
                                          getline(file2,info4);
                                          cout<<"Effect 1:" + info4 << endl;
                                          getline(file2,info5);
                                          cout<<"Effect 2:" + info5 << endl;
                                          getline(file2,info6);
                                          cout<<"Effect 3:" + info6 << endl;
                                          getline(file2,info7);
                                          cout<<"Effect 4:" + info7 << endl;
                                          file2.close();
                                          cin.get();
    I was wondering what is the matter with this as it doesn't print any of the information from the file!

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

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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