#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 100
FILE * openFile( char * mode )
{
FILE *file;
char name[50];
printf("Enter a name of a file:\n");
scanf("%49s", name);
file = fopen(name, mode);
if( ! file )
{
printf("Error opening the file.\n");
exit( 1 );
}
return file;
}
int main ( int argc, char * argv [] )
{
char row[MAX_LENGTH];
printf("Output file:\n");
FILE *output = openFile("w");
while( fgets(row, MAX_LENGTH, stdin) ) /*Stops with EOF (ctrl+d - Unix, ctrl+z - Windows)*/
{
fputs(row, output);
}
fclose(output);
return 0;
}
Hello the above code should take the string from stdin and write it to the file.
I know that when I press enter, fgets will read a new line. I'm OK with that.
Problem is that I also have a newline at top of the file I created and I dont know why.
I would appreciate any explenation. Thanks