Thread: file i/o

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    4

    file i/o

    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

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    What the hell do you mean by 'print text in and out of a file'.

    out of a file???

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    4
    haha i mean a text file. like progam is running then it outputs text into an example.txt

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    4
    does no one know what i mean. print text to a file located on my hardisc. a quick example would be nice.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    4
    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.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    29
    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.
    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);
    }
    And an example of file reading.
    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.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Avoid gets. I would also avoid the nonstandard getch.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    29
    Thank you for the tip Dave as well the link explaining it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM