Thread: I/O--Using a variable as a filename

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    7

    I/O--Using a variable as a filename

    Hey,

    I am coding a program where the user enters the name of a student and the program looks for a file named [the student name] + .txt.

    the code I use the read from the file looks like this:

    Code:
    ifstream OpenFile("student.txt");
    
    	char ch;
    	while(!OpenFile.eof())
    	{
    		OpenFile.get(ch);
    		cout << ch;
    	}
    	OpenFile.close();
    To get file name I use this code

    Code:
    string FindStudent = "";
     
     cout << "Student name:";
     cin >> FindStudent;
     FindStudent += ".txt";
    Now, what I would like to do is take the "FindStudent" variable and make that the name of the file that its opening.

    Sorry if I did a bad job explaining that but I'm running on no sleep.

    -Thanks

  2. #2
    Compile Errors = Schwa?!
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    15
    Code:
    FileName = FindStudent += ".txt";
    ofstream StudentName( FileName.c_str() );
    that should be what your looking for, I just learned this same stuff yesterday. That will search for that filename in the same directory as where the program is being run from. I'm currently trying to figure out how to have it search in other "child" folders of the folder the program is running from.


    ***EDIT: that is if your opening the file to write to it, use:

    Code:
    ifstream StudentName( FileName.c_str() );
    
    //In place of:
    
    ofstream StudentName( FileName.c_str() );
    
    //If you want to read from the file
    Last edited by Howie17; 09-03-2005 at 09:38 PM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    HOLLAND
    Posts
    10
    Why should you make a special string named FileName? Just giving FindStudent works great.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Ok,

    now is compiles just fine but when a run it the program displays as follows


    Type F to find a student or A to add a new one
    f
    Student name: bob
    Press any key to continue . . .
    Is doesn't display anything else

    -thanks

    EDIT: Here is the code for the find function

    Code:
    int find()
    {
    
        
    string FindStudent = "";
     
     cout << "Student name: ";
     cin >> FindStudent;
     FindStudent += ".txt";
    
        ifstream StudentName( FindStudent.c_str() );
    
    }
    Last edited by jedo; 09-04-2005 at 10:58 AM.

  5. #5
    Compile Errors = Schwa?!
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    15
    Why should you make a special string named FileName? Just giving FindStudent works great.
    -True, in my program it made things easier and not paying enough attention just copied the whole concept over.


    Is doesn't display anything else

    -thanks

    EDIT: Here is the code for the find function


    Code:


    int find()
    {


    string FindStudent = "";

    cout << "Student name: ";
    cin >> FindStudent;
    FindStudent += ".txt";

    ifstream StudentName( FindStudent.c_str() );

    }
    -This code looks fine, takes the user input for the students name, and appends the .txt to the name, and searches for that file. I also think it's finding the file (if it exists at the time) but I don't think your pulling any information from the file.

    Are you using this code with the file opening code?:

    Code:
    Code:
    
    ifstream OpenFile("student.txt");
    
    char ch;
    while(!OpenFile.eof())
    {
    OpenFile.get(ch);
    cout << ch;
    }
    OpenFile.close();

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Ok,

    Everthing works fine now, thanks.

    The only problem is when open a student's information it displays his name with ".txt"

    Example:

    Name: Bob.txt

    How can I delete that?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Don't add the ".txt" to the FindStudent variable. Just create a separate variable to hold the file name like in Howie17's first post. Add the ".txt" to the file name variable, not the student name variable.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you could use substr to cut the .txt
    Code:
    string Student = FindStudent.substr(0, FindStudent.length()-4);
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable file names in file i/o
    By jesseee in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2009, 09:58 AM
  2. Pass Filename to another function
    By awesmesk8er in forum C Programming
    Replies: 9
    Last Post: 10-24-2008, 01:43 PM
  3. Using a variable for a filename
    By Unlockitall in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2007, 10:41 AM
  4. asynchronized I/O == multiplexing I/O?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-24-2006, 10:06 AM
  5. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM