Ifstream and String [?]

This is a discussion on Ifstream and String [?] within the C++ Programming forums, part of the General Programming Boards category; Good Afternoon, A quick question? is it possible to this Code: string File_Name[1] = {"Test.txt"}; ifstream inFile(File_Name[0]); if not could ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    44

    Ifstream and String [?]

    Good Afternoon,
    A quick question?

    is it possible to this
    Code:
    string File_Name[1] = {"Test.txt"};
    
    ifstream inFile(File_Name[0]);
    if not could you guys show me a way of doing something similar?

  2. #2
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    You need to put .c_str() after File_Name[0] (one element arrays are basically useless, why are you using one?), which will return the char* which the std::ifstream constructor expects, rather than an std::string.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No that doesn't work. Ifstream doesn't have a constructor that takes a string as parameter.
    This would work
    Code:
    string File_Name = "Test.txt";
    ifstream inFile(File_Name.c_str());
    I changed File_Name to a single string because an array with only one element doesn't make much sense.
    Kurt

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    Basically I have a folder that contains reports for the whole month, and every single day is a separate file.
    What I need to do is find a way to read every single file but one thing the file name will change every day!
    So I'm trying to figure it out since I'm pretty new with C++

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    this is what I have so far which I barely started last night
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void Option_1();
    
    int main()
    {	
    	char selection;
      
    	do
    	{
    		system("cls");
    		cout << "\n\n1 - Option " << endl;
    		cout << "2 - Quit" << endl;
    		cout << "\n Choice: ";
    
      		selection = cin.get();
    		switch( selection)
      		{
      			case '\n':	
    						break;
    	 	 	case '1': Option_1();             				 
    						break;
    			case '2':
    						break;
    			default: cout << '\a';
      		}
        }
    		while( selection != '2' );
    		return 0;
    }
    
    void Option_1() 
    {
    	string line, last;	
    	string myfiles[2] = {"FVT20060301.txt", "FVT20060401.txt"};
    
    	ifstream inFile( myfiles[0].c_str() );
    	
    	while(getline(inFile, line))
    	{
    		if (line != "")
    		{
    			last = line;
    		}
    
    	}
    	cout << last;
    
    	inFile.close();
    	system("pause");
    
    }

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    I will ask for the file name and store it in a array to be able to open an specific file within my directory

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    IF I WANT TO ADD A PATH COULD I DO THIS?

    ifstream inFile( "Billing_Reports\myfiles[0].c_str()" );

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    shure you can add a path. But not this way.
    try
    Code:
    string myfile = "FVT20060301.txt";
    myfile = "Billing_Reports\\myfiles\\" + myfile;
    ifstream inFile(myfile.c_str());
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. problem with string class, ifstream, and getline
    By deathbob in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2005, 12:20 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

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