Code will not read from directed txt file in visual c++!

I have a code here which I do not believe has any errors, but when I try to compile my code it can not read from the file I had saved with the source code. It's just a simple txt file. On top of that I could not read from a .h file I tried to save in the same file as the source code and had to create the header file inside my program. I hope the header file problem will be solved with the txt problem. Here is the code. The code never passes through int Loadmovies()
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "lol.h"
#include<fstream>
#include<iomanip>
#include<iostream>
using namespace std;

int LoadMovies(MovieData[]);
void DisplayMovies(MovieData[], int count);

char FILE_NAME[30] = "MovieData.txt";

int main()
{
    int movieCount = 0;
    MovieData movies[10];
    
    movieCount = LoadMovies(movies);
    
    if(movieCount > 0)
    {
        DisplayMovies(movies, movieCount);              
    }

     system("pause");
     return 0;
}

int LoadMovies(MovieData movie[])
{
    ifstream movieFile;
    int count = 0;
    
    movieFile.open(FILE_NAME);
    
    if(movieFile.good())
    {
	   cout << "good" << endl;

       while(!movieFile.eof())
       {
           movieFile.getline(movie[count].title, 31, '$');
           movieFile.getline(movie[count].director, 26, '$');
           movieFile.getline(movie[count].yearReleased, 5, '$');
           movieFile.getline(movie[count].leadActor, 26, '\n');
           count++;                       
       }
    }
    
    return count;    
}

void DisplayMovies(MovieData movie[], int count)
{
    cout << left;
    cout << setw(31) << "Title"
         << setw(26) << "Director"
         << setw(6) << "Date"
         << setw(25) << "Lead Actor" << endl;
    cout << "-------------------------------------------------------------------------\n"; 
     
    for(int ndx = 0; ndx < count; ndx++)
    {
       cout << setw(30) << movie[ndx].title << " "
            << setw(25) << movie[ndx].director << " "
            << setw(5) << movie[ndx].yearReleased << " "
            << setw(25) << movie[ndx].leadActor << endl;        
            
    }
}


The only response I get when i compile is " Press any key to continue... " Any help is appreciated!
You are declaring FILE_NAME as an array of 30 chars, but you are only using the first 13 positions. This means that the other 17 are garbage data, and when you pass the string to ifstream, it tried to open the filename plus the garbage data.

try this instead:

char* FILE_NAME[] = "MovieData.txt";

or just use std::string:

#include <string>
std::string FILE_NAME("MovieData.txt");
Still no good, I did take into consideration the FILE_NAME may have been getting in the way, and just bypassed the file name and put the file name right in the open file line. I still received the same results! :( could there be a possible problem in some type of settings in c++ that won't allow the directory to work or something?
Last edited on
You are declaring FILE_NAME as an array of 30 chars, but you are only using the first 13 positions. This means that the other 17 are garbage data, and when you pass the string to ifstream, it tried to open the filename plus the garbage data.


This isn't true of arrays which are initialized with values or any arrays at global scope (which have the memory zero'd before main is entered,) and is beside the point since the array is large enough to hold the c-string it is storing and c-strings are terminated by nul characters. open will see nothing beyond the nul character stored therein.

I suspect you just have your data file in the wrong place. If you're using an IDE, one of the easiest ways to figure out the directory you need to place the file in is to open a non-existent file for output and then check through your directories to see where it was created. That is the directory where your data file should be placed. If you're not using an IDE, then the file should just be in the same directory as your executable.

By default, the project directory is set as the working directory in Visual Studio, so it would normally need to be in the projectname/projectname/ directory. One other problem I've seen people run into is hidden file extensions, so what looks like a file named MovieData.txt is actually a file named MovieData.txt.txt.
Last edited on
Thank you so much cire, your last paragraph solved my problem. When I named my files, like "lol.h" it was a text file with my executable file. When I added a .txt to the end of my file locations the program worked. Thank you so much for the help!
Topic archived. No new replies allowed.