C Code Learn - Open file write user input to it
Every once in a while I will get this urge to want to learn some C, C++. I am versed in Python and know my way around bash / Shell scripting like the back of my hand. But as always I am continuing to learn and self teaching myself is how learn the "BEST IS IS". Anyway let's get to it, I wanted a program that would prompt a user for a sentence and then take that sentence and write it to a file, the program would have to create the file if it did not exist. Let me show you the code and attempt to break it down.
jralph@JRALPH2-NB ~
$ cat file_open.c
// Here we do our std library includes to allow the std::library functionality
#include
#include
// Here is where we declare our main function
int main()
{
char c[1024];
FILE *fptr;
fptr=fopen("output.txt","w");
if(fptr==NULL){
printf("Error!!!");
exit(1);
}
printf("Enter a string you wish to write to a file:\n");
gets(c);
fprintf(fptr,c);
fclose(fptr);
return 0;
}