reading from a file

closed account (N8RzwA7f)
hello,
I need help with a program that is supposed to read some values from a file and use those values as input for a function . I didn't include the function as it's irrelevant. My program does not even read the values from the ifstream as the outputfile is blank every time. It just does not seem to read any values form any file .
The data source file is in the same directory (resources file ) . I'm using netbeans.
I also changed the run command to include the data source file name : file , project properties, run command.
the data source file format is for instance: 23 red 34
and the same on the next lines , just different values.
Also , it didn't work with a simple data source file of just numbers and when I tried to just use : while(ins >> numval) {...
Thanks

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
 #include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char** argv) {
    int numval = 0;
    string name = "";
    int numval2 = 0;
    ifstream ins;
    ofstream ofs;
    ins.open(argv[1]);
    if(ins.is_open()){
        cout << "Input file is open\n";
        ofs.open("myfile.txt");
        if(ofs.is_open()){
           cout << "Output file is open\n" ;
        }
    }
    while((ins >> numval) && (ins >> name) && (ins >> numval2) ){
        ofs << numval << name << numval2;
        //function(numval,name,numval2);
    }
    system("PAUSE");
    return 0;
}
Last edited on
What is the exact output of your program?

Does it print "Input file is open"?

Last edited on
closed account (N8RzwA7f)
yes it prints : input file is open
output file is open.
but I'm using a different output file from input file ?
What is the input file name you're using? What is it's contents?

closed account (N8RzwA7f)
the input file is entered with the run command (so argv[1]) and is in the resources folder.
the ofstream opens the output file "myfile.txt" as a seperate file I assume?
the input fiile contents:
20 male 25000
25 male 45000
23 male 30000
16 male 7000
30 male 55000
22 female 27000
26 female 44000
21 female 37000
18 female 17000
29 female 62000
How are you running the program? You need to provide a file name as a parameter to your program. If your parameter is myfile.txt then you will try to open the same file for both input and output.


Edit: Check your while loop.

Do you realize that you can combine input, similar to your output?

Edit2: Something like:
while(sin >> numval >> name >> numval2)

Also note that your output file will have no whitespace between the entries.
Last edited on
closed account (N8RzwA7f)
Yes I'm providing one parameter , the name of the file in the resources folder (data.txt).
and yes thanks I changed the while loop , but it's still not working.
There's still just a blank page for output.
Last edited on
Please post your current code.

When I simplified your program to the following:
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
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char** argv)
{
    int numval;
    string name;
    int numval2;

    std::ifstream sin(argv[1]);
    if(!sin)
    {
        std::cerr << "Error opening the input file.\n";
        return(1);
    }

    while(sin >> numval >> name >> numval2)
    {
        cout <<  numval << " " << name << " " << numval2 << '\n';
    }

    system("PAUSE");
    return 0;
}


I got the following output.

1
2
3
4
5
6
7
8
9
10
11
20 male 25000
25 male 45000
23 male 30000
16 male 7000
30 male 55000
22 female 27000
26 female 44000
21 female 37000
18 female 17000
29 female 62000
sh: PAUSE: command not found

closed account (N8RzwA7f)
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
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char** argv) {
    int numval = 0;
    string name = "";
    int numval2 = 0;
    ifstream ins;
    ofstream ofs;
    ins.open(argv[1]);
    if(ins.is_open()){
        cout << "Input file is open\n";
        ofs.open("myfile.txt", ios::app);
        if(ofs.is_open()){
           cout << "Output file is open\n" ;
        }
    }
    while((ins >> numval >> name >> numval2) ){
        ofs << numval << name << numval2;
        //function(numval,name,numval2);
    }
    system("PAUSE");
    return 0;
}


So why did my code not work? the files opened ? There just was no output.
Also, I tried your code, and the file wouldn't open? Ive had this problem also almost with every version of my code (files not opening).
Thanks
Last edited on
When I run the above program with your input file I get this output:

20male2500025male4500023male3000016male700030male5500022female2700026female4400021female3700018female1700029female62000

One extremely long line of text with no separation of the output.

Are you sure your looking at the "correct" output file? Remember the output file will be in the current working directory and that the current working directory can be in different places depending on how you run the program. That's why I eliminated the output file and just printed the output to the console to see if the program is actually outputting anything.


closed account (N8RzwA7f)
double post.
Last edited on
How exactly are you running your program?

closed account (N8RzwA7f)
I get no output on the screen either.
Here's what I do, I use netbeans. I don't know if you're familiar with netbeans.

in a new project I add the data file in the resource folder. I set the run command to accept one parameter (data.txt) so it's : "{$OUTPUT_PATH}" data.txt
I use : run , compile file //menu
debug, debug project (projectname) //menu.


is it possible, that when it cant open a file it's not in the current working directory? The file (data.txt) is on my desktop , and I added it in the resource folder (add existing item).
and I was looking for the output file in my documents. It does contain the 'myfile.txt' actually created today.

Thanks ! You've been very helpful so far :)
is it possible, that when it cant open a file it's not in the current working directory?

Yes and this is probably the problem.

The file (data.txt) is on my desktop

The desktop will probably never be the default directory.

While I'm not familiar with your IDE you can easily determine where your IDE considers the default directory by creating an output stream (an ofstream has the fewest failure modes) with an unique name. Then use your Operating System's find functionality to locate that file. The location will be the default directory.

Also note that it is better to check the stream state by just using the stream variable name because that checks all failure states, not just one.
1
2
3
ofstream fout("YOURSTRANGEFILENAME");
if(!fout)
   /// Error! 


closed account (N8RzwA7f)
It works! Thanks so much , I was stuck on this forever! :) I found the current directory this way.
Thanks again :)
Topic archived. No new replies allowed.