1

I am trying to load in a text file that contains 1400 rows and 7 columns of data. The data consists of 15 point decimal numbers that have been pasted in to notepad via an excel spread sheet. Each element has a single space between them however this space displays as a tab once pasted from excel. The text file is located directly under my project file in the same directory.

I am using ifstream to load in the text file to then save each data element to an array however the text file consistently fails to open according to my code.

//define input stream
ifstream TrainData;     // 70% training data
TrainData.open("training_dataset_full.txt");
if (TrainData.is_open())
{
   cout << "File successfully open";
}
else
{
   cout << "Error opening file";
}

This consistently outputs Error opening file. Is there something obvious that I am missing?

NOTE: this is where all my text files are located...

File arrangement

  • Where `training_dataset_full.txt` is located? – DimChtz Apr 24 '18 at 09:47
  • @DimChtz it is located in the exact same folder as my QT project is located. – apprenticecoder Apr 24 '18 at 09:48
  • Probably the file does not exist in the current directory (which may depends on your IDE setting amoung other factors). Try to use a full pathname and see if the problem goes away. You can check the current directory as described in [this SO article](https://stackoverflow.com/questions/2868680/what-is-a-cross-platform-way-to-get-the-current-directory)) – Jabberwocky Apr 24 '18 at 09:51
  • I have edited the question with an image of where my text files are located – apprenticecoder Apr 24 '18 at 09:56
  • @apprenticecoder this image is not helpful whatsoever. Do what I suggested in my previous comment. – Jabberwocky Apr 24 '18 at 10:02
  • ... and are you sure that your file _actually_ ends with `.txt` ? – Jabberwocky Apr 24 '18 at 10:03
  • Check if your folder is anywhere that needs admin permission to open. – DimChtz Apr 24 '18 at 10:06
  • @MichaelWalz I have tried both cases with the `.txt` extension and without, both return errors – apprenticecoder Apr 24 '18 at 10:07
  • 1
    @apprenticecoder do what I suggested in my first comment. – Jabberwocky Apr 24 '18 at 10:10
  • Renaming the file to '.txt' will make the file name '.txt.txt', so you should avoid doing that. Sometimes permission issues may cause that too. Checking the paths of both the text files and your program might help, as well. –  Apr 24 '18 at 10:16
  • I have it working, I had to specify the exact path to the file when calling it. Thank you for all your helpful comments! – apprenticecoder Apr 24 '18 at 10:31

1 Answers1

1

instead of putting the file in the project folder put it in the debug / release folder. as the executable will look into the folder where it is and not the source code

EYakoumi
  • 407
  • 6
  • 17