I'm new to C programming and I was doing a simple project in C where I have to write a function to take file name as input and allow them to write the file with the limitation of 10,000 lines. Here is the function.
Code:
void write_mode(void)
{
char filepath[300];
char text[1000];
printf("\t\t\t Filename: ");
scanf("%s",filepath);
system("clear");
FILE *file;
file = fopen(filepath,"w");
printf("\n\n");
for(int i = 1;i < 10001;i++)
{
printf("%d ",i); // the bug
fgets(text,1000,stdin);
if (strncmp(text,":save\n",1000) == 0)
{
system("clear");
printf("\t\t\t\t Saved\n");
printf("\t\t\t\t Press Enter to continue");
getchar();
fclose(file);
break;
}
else if (strncmp(text,":exit\n",1000) == 0)
{
fclose(file);
break;
}
else if (strncmp(text,":n\n",1000) == 0)
{
fprintf(file,"%s","\n");
continue;
}
else if (strncmp(text,"\n",1000) == 0)
continue;
else
{
fprintf(file,"%s",text);
continue;
}
}
}
[ Pardon me if you are finding it difficult to read the code and for the typos ]
What's happening here:
1. Taking the file name as input
2. Prompting user to write some data,allowing them to use to some additional keywords
3. Printing the line number.
So here,loop should execute from 1, print the line number and wait for user input to move to the next line. for example,
Expected Output:
1 [waiting for input]
but instead,I get this
Output:
1 2 [waiting for user input]
Why is this happening and how can I solve this issue ? Also any sort of suggestions regarding this will be much appreciated . Thank you :)