Click here to Skip to main content
15,920,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, I've been working on a project and I'm having problems with the very beginning of it. I would appreciate any help.

So in this task, I should get my input from a file and from what I've learned I can do that using ifstream. I wrote a condition that if the file didn't open send me an error message.
and my problem is, it doesn't work and I keep getting the error message and it seems like it can't open the file and I can't find out why it won't open the file.
///////////////////////// my header file
#ifndef header_h
#define header_h


#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

struct cam{
    string  name;
    string post;
    int age;
    int salary;
};

void data_input(string fn1, vector<cam> & W);         /*input data from our input file*/
#endif
/////////////////////////


/////////////////////////the main
#include <iostream>
#include <fstream>
#include "header.h"
using namespace std;

string inputfile="input.txt";
string outfile="output.txt";
vector<cam> employee_details;

int main()
{
    data_input(inputfile, employee_details);
    return 0;
}
/////////////////////////


/////////////////////////this the cpp file 
#include <stdio.h>
#include <iostream>
#include <vector>
#include <fstream>
#include "header.h"

using namespace std;

void data_input(string fn1, vector<cam> &W){
    string tmp;
    ifstream f1( fn1.c_str() );
   
    if( f1.is_open() )
    {
        while( f1.good() ))
        {
            //reading names
            getline (f1,tmp,';');
            cam tmp_cam;
            tmp_cam.name=tmp;
            W.push_back(tmp_cam);
            
            //reading the ages
            getline(f1,tmp,';');
            W.back().age = atoi( tmp.c_str() );
            
            //reading the ranks
            getline (f1,tmp,';');
            W.back().post = tmp;
            
            //reading the salaries
            getline (f1,tmp,';');
            W.back().salary = atoi( tmp.c_str() );
            
        }
      
        f1.close();
    }
    else
    {
        cout << "Error to open the file" <<  endl;
    }
    
}
/////////////////////////


What I have tried:

every thing I could find on the net but I feel I'm missing something really small that's why it came to my mind why not sharing it here and see it from different views
Posted
Updated 12-Apr-18 22:48pm
Comments
Richard MacCutchan 13-Apr-18 4:03am    
Most likely the file does not exist in the working directory of your application.
amirak20 13-Apr-18 8:57am    
that was the first thing I did, it can't be the case.
Richard MacCutchan 13-Apr-18 9:12am    
Well it must be something similar. I just built and ran your code and it works fine: after removing the extra close parenthesis on your while statement.

You can print a system error message when opening fails:
C++
ifstream f1( fn1.c_str() );
if ( f.fail() )
    cerr << "Failed to open file: " << strerror(errno);
As already noted by Richard the file may not exist in the current directory. Then you should move or copy it or use absolute paths for your files.
 
Share this answer
 
Comments
amirak20 13-Apr-18 9:36am    
I tried this code and yes the problem is the file, but I have copied the file in the main directory with the same name as the one which I've entered in the program.
Jochen Arndt 13-Apr-18 9:45am    
When using a file name without path, the current working directory is used. That may be still not your "main directory".

When executing an application from the command line, the working directory is those (usually) shown at the command prompt.

When executing an application from within an IDE, the working directory is usually the directory of the executable. With Visual Studio for example it is not your project main directory but the Release resp. Debug sub directory.

To show the current working directory you can use the getcwd() function in your application.
amirak20 13-Apr-18 11:36am    
thanks for your help getcwd() function helped and now my program is working.
Looks like your app isnt finding the file. Better to say that the file isnt the directory of the running process. This could differ, so better take a look in the setings of the project.

As always it is best to use the complete path for file operations. So also for the output process!!!

Learn to use the debugger. This debugger tutorial is providing some basic knowledge.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900