After you type the filename on screen you press enter to continue, so isn't that always a newline? It does work now bytheway.
Could you help me with another problem too? It's still about the same program, I could make a new thread, but before you know this whole forum is stuffed with my questions...
I'm having trouble constructing a loop that keeps asking for a filename until the filename given, exists.
This is what I came up with, but crashes once you give the filename that exists (after giving an incorrect one):
Code:
printf("Give the name of the file you wish to analyze.\n");
printf("-> ");
fgets(datafile, 1024, stdin);
datafile[strlen(datafile)-1] = '\0';
printf("\n");
memcpy(filenames[0], datafile, 1024); //put datafile in filenames array
FILE* file0 = fopen(filenames[0], "r");
if (file0 == NULL)
{
while (filefound == 0)
{
printf("File '%s' not found...\n", filenames[0]);
printf("Give the name of the file you wish to analyze.\n");
printf("-> ");
fgets(datafile, 1024, stdin);
datafile[strlen(datafile)-1] = '\0';
printf("\n");
memcpy(filenames[0], datafile, 512);
FILE* file0 = fopen(filenames[0], "r");
if (file0 == NULL)
{
filefound = 0;
}
else
{
filefound = 1;
}
}
}
else
{
lines_in_file0 = Linecount(filenames, 0);
printf("No of lines in %s: %d \n", filenames[0], lines_in_file0);
rewind(file0);
//for (i=0;i<(lines_in_file0/3);i++)
// {
// Analyse3(file0, Startypes);
// printf("Lines analyzed: %d \n", (i*3));
// }
}