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 does even display the final output of "("Job successful, hit any key to exit and press enter".Below are the code and output, please advise:
//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 number 2 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;
Here is the output
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