Good Afternoon,
A quick question?
is it possible to this
if not could you guys show me a way of doing something similar?Code:string File_Name[1] = {"Test.txt"}; ifstream inFile(File_Name[0]);
Good Afternoon,
A quick question?
is it possible to this
if not could you guys show me a way of doing something similar?Code:string File_Name[1] = {"Test.txt"}; ifstream inFile(File_Name[0]);
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
No that doesn't work. Ifstream doesn't have a constructor that takes a string as parameter.
This would work
I changed File_Name to a single string because an array with only one element doesn't make much sense.Code:string File_Name = "Test.txt"; ifstream inFile(File_Name.c_str());
Kurt
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++
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"); }
I will ask for the file name and store it in a array to be able to open an specific file within my directory
IF I WANT TO ADD A PATH COULD I DO THIS?
ifstream inFile( "Billing_Reports\myfiles[0].c_str()" );
shure you can add a path. But not this way.
try
KurtCode:string myfile = "FVT20060301.txt"; myfile = "Billing_Reports\\myfiles\\" + myfile; ifstream inFile(myfile.c_str());