It is very similar to the original but I have replaced fgets() with getline().
It is interesting because whenever I run the file through the simpler code below, it does print the "\n" character.
Simple test code:
Code:
while (!feof(file))
{
characters = getline(&buffer, &bufsize, file);
if (string(buffer) == "\n")
{
printf("You typed:\\n");
}
printf("%zu characters were read.\n", characters);
printf("You typed: '%s'\n", buffer);
}
Current code:
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)
{
char *buffer;
size_t bufsize = 32;
size_t characters;
buffer = (char *)malloc(bufsize * sizeof(char));
characters = getline(&buffer, &bufsize, file);
sscanf(buffer, "%d", &numIsolated);
cout << numIsolated << endl;
if (numIsolated > mmBytes || numIsolated < 0)
{
cout << "File not valid" << endl;
reading = 0;
break;
}
else
{
addArr[0] = numIsolated;
}
}
else if (numLinesRead == 1)
{
char *buffer;
size_t bufsize = 32;
size_t characters;
buffer = (char *)malloc(bufsize * sizeof(char));
characters = getline(&buffer, &bufsize, file);
if (string(buffer) != "\n")
{
cout << "File invalid" << endl;
reading = 0;
}
}
else
{
while (!feof(file))
{
numAddresses = addArr[0];
char *buffer;
size_t bufsize = 32;
size_t characters;
buffer = (char *)malloc(bufsize * sizeof(char));
characters = getline(&buffer, &bufsize, file);
char lineStr[100];
sscanf(buffer, "%s %d", lineStr, &numIsolated);
cout << endl
<< string(lineStr) << endl;
cout << numIsolated << endl;
if (numIsolated > mmBytes || numIsolated < 0)
{
if (numLinesRead != 1)
{
cout << "File contains invalid address" << endl;
reading = 0;
break;
}
}
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++;
cout << endl
<< numLinesRead << endl;
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;
}
}
}