CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2009
    Posts
    21

    Difficulty reading from two files simultaneously

    I am trying to write a program that

    1. opens letterFile containing letters on each line
    2. reads in first line of letterFile
    3. save the pointer position of letterFile in pos
    4. opens numFile containing numbers on each line
    5. as it goes through numFile, outputs the letter from the first line of letterFile next to each line of numFile
    6. close numFile
    7. sets the position of letterFile to pos
    8. repeats step two till end of letterFile

    Problem is, the program only reads in first line of letterFile, and then terminates. Plus, it doesn't even display the final output of "("Job successful, hit any key to exit and press enter". Below are the code and output, please advise:

    Code:
    //position of the file pointer
    int pos=0;
    
    //If you can open file with letter, go through file till the very end
    while (!letterFile.eof()) 
    
    {
    	getline (letterFile,letterLine);
    	pos = letterFile.tellg();
    	int TempNumOne=letterLine.size();
    	char letterOutput[100]={0};
    
    	for (int a=0;a<=TempNumOne;a++)
    	{
    	 	letterOutput[a]=letterLine[a];
    	}
    
    	sscanf(letterOutput, "%s", &letter);
    		
    	//Check if you can open file with two numbers
    	numFile.open(numFilename, ios::in);
    	
    	if (!numFile) 
    	{
    		cerr << "Can't open input file " << numFilename << endl;
    		exit(1);
    	}
    			
    	
    	while(!numFile.eof()) 
    	{//Loop through file with two numbers
    
    		getline (numFile,numLine);
    		int TempNumTwo=numLine.size();
    		char numOutput[100]={0};
    
    		for (int b=0;b<=TempNumTwo;b++)
    		{
    
    			numOutput[b]=numLine[b];
    
    		}
    
    		sscanf(numOutput, "%s %s", &num1, &num2);
    		printf("letter is %s, number1 is %s and number2 is %s \n" letter,  num1,  num2);
    
    	}//End loop through file with two numbers
    
    	numFile.close();
    	letterFile.seekg(pos);
    
    }//End loop through file with letter addresses
    letterFile.close();
    
    printf("Job successful, hit any key to exit and press enter.\n");
    scanf("%d", &counterx);
    
    return 0;

    Code:
    letter is sdfsdg, number1 is 1 and number2 is 3
    letter is sdfsdg, number1 is 4 and number2 is 5
    letter is sdfsdg, number1 is 7 and number2 is 8
    letter is sdfsdg, number1 is 5 and number2 is 9
    letter is sdfsdg, number1 is 1 and number2 is 3
    letter is sdfsdg, number1 is 4 and number2 is 5
    letter is sdfsdg, number1 is 3 and number2 is 9
    letter is sdfsdg, number1 is 2 and number2 is 9
    letter is sdfsdg, number1 is 2 and number2 is 5
    letter is sdfsdg, number1 is 3 and number2 is 5
    letter is sdfsdg, number1 is 3 and number2 is 9
    letter is sdfsdg, number1 is 5 and number2 is 4
    letter is sdfsdg, number1 is 9 and number2 is 2
    letter is sdfsdg, number1 is 9 and number2 is 4
    letter is sdfsdg, number1 is 7 and number2 is 8
    letter is sdfsdg, number1 is 5 and number2 is 9
    letter is sdfsdg, number1 is 1 and number2 is 3
    letter is sdfsdg, number1 is 3 and number2 is 9
    letter is sdfsdg, number1 is 2 and number2 is 9
    letter is sdfsdg, number1 is 2 and number2 is 5
    letter is sdfsdg, number1 is 3 and number2 is 5
    letter is sdfsdg, number1 is 3 and number2 is 9
    letter is sdfsdg, number1 is 5 and number2 is 4
    letter is sdfsdg, number1 is 9 and number2 is 2
    letter is sdfsdg, number1 is 9 and number2 is 4
    letter is sdfsdg, number1 is 4 and number2 is 5
    letter is sdfsdg, number1 is 7 and number2 is 8
    letter is sdfsdg, number1 is 5 and number2 is 9
    letter is sdfsdg, number1 is 1 and number2 is 3
    letter is sdfsdg, number1 is 4 and number2 is 5
    letter is sdfsdg, number1 is 7 and number2 is 8
    letter is sdfsdg, number1 is 5 and number2 is 9
    letter is sdfsdg, number1 is 1 and number2 is 3
    letter is sdfsdg, number1 is 4 and number2 is 5
    letter is sdfsdg, number1 is 7 and number2 is 8
    letter is sdfsdg, number1 is 3 and number2 is 9
    letter is sdfsdg, number1 is 2 and number2 is 9
    letter is sdfsdg, number1 is 2 and number2 is 5
    letter is sdfsdg, number1 is 3 and number2 is 5
    letter is sdfsdg, number1 is 3 and number2 is 9
    letter is sdfsdg, number1 is 5 and number2 is 4
    letter is sdfsdg, number1 is 9 and number2 is 2
    letter is sdfsdg, number1 is 9 and number2 is 4

    V/r,
    RSA

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Difficulty reading from two files simultaneously

    I am having a litle trouble following everything but ..

    1) What is the purpose of the tellg() / seekg() ?

    2) The following type of construct will loop one too many times,
    since eof() is not true until there is an actual attempt to read past EOF.

    Code:
    while(!numFile.eof()) 
    {//Loop through file with two numbers
    	getline (letterFile,letterLine);
    Change these to:

    Code:
    while ( getline (numFile,numLine) )
    {

    3) After reading processing the first line in letterFile, it will never be successful
    in opening the numFile a second time. To solve this problem, you should
    desclare the stream object when you use it if possible.

    Code:
    ifstream numFile(numFilename, ios::in);
    	
    if (!numFile) 
    {
    	cerr << "Can't open input file " << numFilename << endl;
    	exit(1);
    }
    4) If the second file contains numbers, why reading them in as strings ?

    Code:
    int number1 , number2;
    
    while (numFile >> number1 >> number2)
    {

    5) Why mix C and C++ outputs so much (printf , ifstream , cerr). Since you are
    coding in C++, I would stick to the C++ I/O, unless there is a specific reason
    not to.


    6) what type is letter ?

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Difficulty reading from two files simultaneously

    Quote Originally Posted by RSASKA View Post
    I am trying to write a program that

    1. opens letterFile containing letters on each line
    2. reads in first line of letterFile
    3. save the pointer position of letterFile in pos
    4. opens numFile containing numbers on each line
    5. as it goes through numFile, outputs the letter from the first line of letterFile next to each line of numFile
    6. close numFile
    7. sets the position of letterFile to pos
    8. repeats step two till end of letterFile
    Let's simplify this whole thing, since no one knows what's in those files except for yourself.
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        ifstream letterFile;
        ifstream numFile;
        char letterLine[200];
        char numLine[200];
    
        letterFile.open("whatever1.txt");
        while (!letterFile.eof()) 
        {
             letterFile.getline(letterLine, 199);
             cout << letterLine << endl;
    
             numFile.open("whatever2.txt");	
             while(!numFile.eof()) 
             {
                 numFile.getline(numLine, 199);
                 cout << numLine << endl;
             }
        }
    }
    What does this produce? Since you say the problem is reading from two files simultaneously, and you didn't provide a full code example, you should first write a simple program that reads those two files. Once you get that to work, then you introduce other logic to your code.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Dec 2009
    Posts
    21

    Re: Difficulty reading from two files simultaneously

    Paul, I tried out the code, it compiles and when it runs, I see the terminal screen pop-up with it listing characters before exiting. I'll introduce the other logic to get it fully functional.


    Philip, tellg() was to get the position of the pointer in the first file, and seekg() was to set it. I thought that there is only one file pointer and once a second file is open, the file pointer moves to the second file and looses track of the first file. Also, it is easy to get confused between C and C++, especially when trying to get code to work. But let me just stick to C++.

    I'll let you know how it works out, Thanks!
    V/r,
    RSA

  5. #5
    Join Date
    Dec 2009
    Posts
    21

    Re: Difficulty reading from two files simultaneously

    Good Morning,

    I added logic to the code such that:

    1. prompt user for name of letter file
    2. prompt user for name of number file
    3. check whether letter file can be open
    4. close letter file
    5. check whether number file can be open
    6. close number file
    7. read line from letter file
    8. read line from number file
    9. type file from letter file next to line from number file.

    herein lies the problem: it only reads letter file once and iterates through the number file once. I have copied and pasted the code and the output. I've tried every possible remedy since early this morning, please advise.


    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	ifstream letterFile;
    	ifstream numFile;
    	char letters[20];
    	char letterLine[20];
    	char numbers[200];
     	char numLine[200];
    	int counterx;
    
    	cout << "Please type in the name of the letter file: \n";
    	cin >> letters;
    
    	cout << "Please type in the name of the number file: \n";
    	cin >> numbers;
    
    	letterFile.open(letters);
    	if (!letterFile) 
    	{
    		cerr << "Can't open input file " << letters << endl;
    		exit(1);
    	}
    	letterFile.close();
    
    	numFile.open(numbers);
    	if (!numFile) 
    	{
    		cerr << "Can't open input file " << numbers << endl;
    		exit(1);
    	}
    	numFile.close();
    
    	letterFile.open(letters);
        	while (!letterFile.eof()) 
        	{
    		letterFile.getline(letterLine, 19);
    
    		numFile.open(numbers);
    		while(!numFile.eof()) 
    		{
    			numFile.getline(numLine, 199);
    			cout << letterLine << " " << numLine << endl;
    		}
    		numFile.close();
    	}
    
    	cout << "Job successful, hit any key to exit and press enter.\n";
    	cin >> counterx;
    }
    Code:
    Please type in the name of the letter file:
    letters.txt
    Please type in the name of the number file:
    numbers.txt
    adsfasdgasdf 1 3
    adsfasdgasdf 4 5
    adsfasdgasdf 7 8
    adsfasdgasdf 5 9
    adsfasdgasdf 2 9
    adsfasdgasdf 2 5
    adsfasdgasdf 3 5
    adsfasdgasdf 3 9
    adsfasdgasdf 5 4
    adsfasdgasdf 9 2
    adsfasdgasdf 9 4
    Job successful, hit any key to exit and press enter.
    V/r,
    RSA

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Difficulty reading from two files simultaneously

    1) Are you positive that it only reads the first line from the letter file ?
    I don't see why it would not read the other lines.

    2) As I mentioned in my first post, it will only go thru the number file once.
    It will fail to even open the file the second time thru the letter-file loop.
    It is almost always best to declare the streams when you use them ... that
    way you will not have that problem.

    3) Instead of doing that, you can clear the stream's state. Add the indicated line
    to your code:

    Code:
    numFile.clear();  // add this line
    
    numFile.open(numbers);
    while(!numFile.eof())

  7. #7
    Join Date
    Dec 2009
    Posts
    21

    Re: Difficulty reading from two files simultaneously

    I added numFile.clear(); and it works, thanks!
    V/r,
    RSA

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured