Thread: Input/Output

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    Question Input/Output

    I am having a problem with input/output. I have created a text file called input.txt and want to move this to output.txt
    I have been able to move a single character over with %c but when I have tried to use %s it doesn't appear to work.

    This is the code I have used:

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>

    void main (void)
    {
    char e[30];
    char temp_char;
    int counter;

    FILE *f_input;
    FILE *f_output;

    for(counter=0; counter<200; counter++)

    if ((f_input = fopen("input.txt","r"))==NULL)
    {
    printf("Cannot open file\n");
    exit(1);
    }

    fscanf(f_input,"%c", &temp_char[counter]);
    if ((f_output = fopen("output.txt","w"))==NULL)
    {
    printf("Cannot open file for writing\n");
    exit(1);
    }

    fprintf(f_output,"The input file contained the following: %c \n",temp_char);

    fclose(f_input);
    fclose(f_output);

    getch();
    }

    Can anyone help, thanks if you can.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    try this
    Code:
    int main()
    {
         int ch;
         FILE *f_input, *f_output;
    
         if ((f_input = fopen("input.txt","r"))==NULL) 
        { 
              printf("Cannot open file\n"); 
              exit(1); 
        } 
       
        if ((f_output = fopen("output.txt","w"))==NULL) 
        { 
              printf("Cannot open file for writing\n"); 
              exit(1); 
        }  
    
         
    
         while((ch = fgetc(f_input)) != EOF)
              fputc(ch, f_output);
    
         fclose(f_input);
         fclose(f_output);
         return 0;
    }
    look up fgetc, fputc and EOF in your documentation
    Last edited by C_Coder; 12-02-2001 at 07:30 AM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    2
    Thanks for that - It is now working.

    Out of interest, if I wished to transfer message over but this time backwards will I be ok to use a'For' loop?

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Of course.

    You just read the message from the end to the start and print it out on the screen.
    Last edited by GaPe; 12-02-2001 at 08:15 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with input/output files
    By aldin176 in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2006, 09:04 AM
  2. Input/Output Streams
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 12-23-2005, 02:58 PM
  3. input/output
    By JCK in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2002, 12:07 PM
  4. filestreams (fstream) and standard input/output (stdlib)
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 10:14 AM
  5. FILE input/output and ARRAY
    By poorman in forum C Programming
    Replies: 1
    Last Post: 03-05-2002, 04:17 PM