how can i print text in and out of a file. maybe someone can give a quick example of somne functions io could use. thanx
how can i print text in and out of a file. maybe someone can give a quick example of somne functions io could use. thanx
What the hell do you mean by 'print text in and out of a file'.
out of a file???
haha i mean a text file. like progam is running then it outputs text into an example.txt
does no one know what i mean. print text to a file located on my hardisc. a quick example would be nice.
An example would be nice, true. Using the search button would be nice too. Or perhaps look at your C book. That'd do the job also. Don't tell me you're trying to learn C without any books or without bothering to search for anything. Surely you've read about fread/fwrite/fputc/fgetc/fputs/fgets/fopen/fclose? No? Better get started.
Quzah.
Hope is the first step on the road to disappointment.
ok good enough at least i know where to start looking. I was getting a little confused when i looked up c programming file i/o. becasue in unix everything is a file and i didnt know if they were talking about actual text files or not.
Here are two very basic examples of file i/o. I don't like to do this because it defeats the purpose of learning but this was how I learned.
And an example of file reading.Code:/*small example of file writing one character at a time Author: Nate Ward*/ #include<stdlib.h> #include<stdio.h> main(){ FILE *fd; char file[20]; int c; printf("Enter the name of the file you want to open\n>>>"); gets( file ); if((fd=fopen(file, "w")) == NULL){ printf("Could not open %s.\n", file); getch(); exit(1); } printf("Enter your text.\n Press '`' to quit.\n>>>"); while((c=getch())!='`'){ fputc(c,fd); putchar(c);} printf("\nThank you.\n"); fclose(fd); getch(); exit(0); }
Code:/*This is an example of file reading. Author: Nate Ward*/ #include<stdlib.h> #include<stdio.h> main(){ FILE *fd; int c,ch; char file[20]; ch=0; printf("Enter the name of an existing .txt file.\n>>>"); gets( file ); if((fd=fopen(file, "r"))==NULL){ printf("Could not open %s.\n",file); getch(); exit(1); } while((c=fgetc(fd))!=EOF){ putchar(c); ch++;} printf("\n%d characters read. Thank you.\n",ch); fclose(fd); getch(); exit(0); }
Last edited by WolfPack; 12-04-2002 at 01:16 PM.
Avoid gets. I would also avoid the nonstandard getch.
Thank you for the tip Dave as well the link explaining it.