input file

Hi guys I am writing a C++ program on Linux that will be able to open and gather certain info from a set of files located on a file on my home directory. This file is named dmf.out Now all this file contains is the paths to these files or file names. For example: /home/int/sstf/int_02JA_006/target/ssf_host/datastores_1/02JA/datastore files and so on..... Now the program I have written so far does manage to open the first file in dmf.out and gather the required info I want from the first file. The problem is that the second file or the next one in line will not open. Therefore when I run my program (string info) repeats and repeats to be outputted. In my source code below I made a seperate file (dmf2.out) that only contains two files so that I can test this program.

#include <iostream>
#include <fstream>
#include <string>
#include <ctring> //dont't know if I need this

using namespace std;

int main ()
{

ifstream inData;
ifstream inDmf;
string file, dmf, info;
int counter=0;
inData.open("dmf2.out");
inData>>file //prime in read

while (inData)
{
inDmf.open(file.c_str()); /*this is the problem I think the value of file will not change here*/
while (inDmf)
{
getline (inDmf, dmf, '~'); // reads the file as a whole string
info= dmf.substr(0,dmf.find("SSF_HOST"));/* only pulls out certain info up to "SSF_HOST" inside of contents */
}

inDmf.close();
cout<<info<<endl; //outputs the info I want
cout<<file<<endl // file here is still the first file from dmf2.out
counter++; //counts how many files are in dmf2.out
inData>>file;
cout<<file<<endl; // file here is the second file from dmf2.out
}
cout<<counter<<endl; /*outputs a value of 2 becuase dmf2.out only contains 2 files */


return 0;
}


Output: these 7 things

1. the string info-which is the information I wanted from the file contained in dmf2.out

2.the string file- which here is still the first file from dmf2.out
3. the string file-(after it has gone through the command inData>>file) -which is now the second file of dmf2.out.
4. the string info-same as above ^^^^^^
5. the string file - which is still the second file of dmf2.out,
6. the string file - same as above^^^^^^^
7. int counter - which a 2 is outputted

I think the "inDmf.open(file.c_str());" command will not use the new file after file has under gone the command "inData>>file". I have looked up the command c_str(); in the library and maybe the reason it does not work is you can not modify the file.c_str() command once it has been executed. Any adivce or help would be highly appreciated becuase I have spent nearly the whole day looking for another way or command how to do it. THANK YOU VERY MUCH FOR READING THIS!!

You're file object is declared at the wrong scope:

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
int main ()
{
	string info;
	int counter=0;

	ifstream inData("dmf2.out");
	while (inData)
	{
		string file;
		inData>>file //prime in read
		ifstream inDmf(file.c_str());
		while (inDmf)
		{
			string dmf;
			getline(inDmf, dmf, '~'); // reads the file as a whole string
			info = dmf.substr(0,dmf.find("SSF_HOST"));/* only pulls out certain info up to "SSF_HOST" inside of contents */
		}

		inDmf.close();
		cout<<info<<endl; //outputs the info I want
		cout<<file<<endl // file here is still the first file from dmf2.out
		counter++; //counts how many files are in dmf2.out
		inData>>file;
		cout<<file<<endl; // file here is the second file from dmf2.out
	}
	cout<<counter<<endl; /*outputs a value of 2 becuase dmf2.out only contains 2 files */

	return 0;
}
Last edited on
Thank you very much for you reccomendation. I tried your method and the only ouput i got was the string file changing as it undergoes (inData>>file). So there was no string info output at all. I mean there was but for some reason it was just a blank line. I tried changing the argument in the while loops to while(! inData.eof() ) while(! inDmf.eof() ) This method got me the output of:
1. the string info but only for the first entry in "dmf2.out"
2. the string file twice as mentioned above ^^^
3. I got no ouput of int counter

I highly apprecitae the help and there has to be another way of reading the the files inside of "dmf2.out". I still think the call "file.c_str()" will not change and there has to be another way for ifstream to read it. If there is any other method or way you can reccomend I would be of much help to me.
Instead of:
inData>>file //prime in read

it's customary to read the whole line with:
std::getline(inData, file); //prime in read

The former reads up to the first white space.
Topic archived. No new replies allowed.