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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
jralph@JRALPH2-NB ~ $ cat file_open.c // Here we do our std library includes to allow the std::library functionality #include<stdio.h> #include<stdlib.h> // 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; } |