How to verify a blank line in a file?
I'm trying to verify that a file is in a very specific format.
It should have a single number (n) on the first line, the second line is blank, each of the following lines have "R" or "W" followed by a number that's within a given range. There should be n non-blank lines after the first.
The issue I'm having is with verifying the blank line. I check to see if fgets() of the line is NULL when the number of lines read is 1, but it seems to skip that line and gives the output "file formatting is invalid 4"
Code:
int reading = 1;
int n = 0;
file = fopen(fileName, "r");
if (file == NULL) // check if the file exists
{
cout << "File not found" << endl;
}
else if (file != NULL) // if the file does exist
{
char fileLine[100];
int numIsolated = 432e6;
int numLinesRead = 0;
if (numLinesRead == 0)
{
fgets(fileLine, 100, file);
sscanf(fileLine, "%d", &numIsolated);
cout << endl
<< numIsolated << endl;
if (numIsolated > mmBytes || numIsolated < 0)
{
cout << "File not valid" << endl;
reading = 0;
break;
}
else
{
addArr[0] = numIsolated;
}
}
else if (numLinesRead == 1)
{
if (fgets(fileLine, 100, file) != NULL)
{
cout << "File invalid" << endl;
reading = 0;
break;
}
}
else
{
while (fgets(fileLine, 100, file) != NULL)
{
numAddresses = addArr[0];
char lineStr[100];
sscanf(fileLine, "%s %d", lineStr, &numIsolated);
cout << endl
<< string(lineStr) << endl;
cout << numIsolated << endl;
if (numIsolated > mmBytes || numIsolated < 0)
{
cout << "File contains invalid address" << endl;
reading = 0;
}
else
{
if (string(lineStr) != "R" && string(lineStr) != "W")
{
cout << "file formatting is invalid 2" << endl;
reading = 0;
break;
}
else
{
n++;
addArr[n] = numIsolated;
printf("\naddArr[%d] = %d", n, addArr[n]);
if (string(lineStr) == "R")
{
addArr[n + numAddresses] = 1;
printf("\naddArr[%d] = %d", n + numAddresses, addArr[n + numAddresses]);
}
else
{
addArr[n + numAddresses] = 2;
printf("\naddArr[%d] = %d", n + numAddresses, addArr[n + numAddresses]);
}
}
}
numLinesRead++;
numIsolated = 432e6;
}
}
if (reading == 1)
{
if (n != 0)
{
if (n != numAddresses)
{
cout << "n = " << n << endl
<< "numAddresses = " << numAddresses << endl
<< "file formatting is invalid 3" << endl;
reading = 0;
}
else if (n == numAddresses)
{
validFile = 1;
cout << endl
<< "n = " << n << endl
<< "numAddresses = " << numAddresses << endl
<< "arr[1] = " << addArr[1];
}
}
else
{
cout << "file formatting is invalid 4" << endl;
reading = 0;
}
}
}