Getting data from a file

I'm trying to figure out how to get text from a file, but the user inputs the file name so they can change which file they can use. I wrote a short little code that should at least output the first word of the file, but nothing. I'm new to using files in programming so any help would be great!

I'm using a .txt file with made up stock information on it:
FedEx Corp (FDX)
112.57 .53 100
113.77 1.5 75

Time Warner Inc (TWX)
65.78 -.4 100
66.2 -.6 75

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
   
   string fileName;
   ifstream inputFile;
   string stock;
   
   cout << "Enter file name: ";
   cin >> fileName;
   
   inputFile.open(fileName);
   
   inputFile >> stock;
   cout << stock << endl;
   
   inputFile.close();
   
   return 0;
}
Last edited on
Are the files in the same directory as where you're running the program from? Do the files have an extension you can't see because windows is hiding the truth from you?
The file is in the folder with the .cpp file of the program, is that the directory? By extension do you mean the .txt at the end?
@MarketAnarchist: that's not the problem here.

@toast9: No, the location of the cpp file has nothing to do with your program's working directory. Can you tell me how you run your program? Do you use an IDE? Or the commandline?
commandline
So I looked around for the directory and found it. I'm using Xcode to write the program and its default directory is really really buried. I changed the directory to a folder I made in the project fold, but it still doesn't work. Any ideas?

is there a way to choose a new path where the .txt file comes from?
Last edited on
In your command line, run
ls
and see if your text files are listed.
I don't know where the command line is, but I did finally found where the directory is (Xcode/DerivedData/NAME_OF_PROJECT/Build/Products/Debug/) and put my file in there and it works! Thanks for the help. :)
Topic archived. No new replies allowed.