Thread: Search for .txt file

  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
    28,413
    I think it should be:
    Code:
    ofstream a_file(("Ingrediants\\" + filename + ".txt").c_str());
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,079
    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
    28,413
    Could be some problem with file1. How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    getline() reads from an input stream. file1 is an output stream.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    so i just need to open the file for reading from then im sorted
    Use std::ifstream?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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!

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, 02:53 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10: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, 10:33 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM