Thread: Help with fscaf() to getc() and fprintf() to putc()

  1. #1
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30

    Help with fscaf() to getc() and fprintf() to putc()

    I need to modify the below program by changing from:

    fscaf() to getc()

    and

    fprintf() to putc()

    .... i am unfamiliar with getc/putc, and have tried messing with changing out a few things, only to find errors during compile.

    heres my code..
    thanks in advance:

    Code:
    #include <stdio.h>
    
    int main()
    {
       FILE *afile, *anotherfile;
       int number;
    
       afile=fopen("fun-with-numbers.txt","r");
       anotherfile=fopen("more-fun-with-numbers.txt","w");
    
       if (afile==NULL)
       {
          printf("Could not open file for reading.\n");
       }
       else
       {
          fprintf(stdout,"Success opening file.\n\n");
          fscanf(afile,"%d", &number);
          while (feof(afile)==0)
          {
             printf("%d\n", number);
             fprintf(anotherfile,"%d\n", number);
             fscanf(afile,"%d", &number);
          }
          /* do not pass a NULL pointer to fclose() */
          fclose(afile);
       }
       return 0;
    }
    Also, my instuctions ask me to "Use fun-with-chars.txt as the input file and more-fun-with-chars.txt as the output file."

    here is the given fun-with-chars.txt input file:

    Code:
    100 99 98 97 96 95 94 93 92 91 90
    Last edited by otchster; 10-30-2005 at 07:21 PM.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    This looks like the original code. Where is the code you made when trying to change it, that produces the errors?

  3. #3
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    Code:
    
    #include <stdio.h>
    
    int main()
    {
       FILE *afile, *anotherfile;
       int number;
    
       afile=fopen("fun-with-numbers.txt","r");
       anotherfile=fopen("more-fun-with-numbers.txt","w");
    
       if (afile==NULL)
       {
          printf("Could not open file for reading.\n");
       }
       else
       {
          fprintf(stdout,"Success opening file.\n\n");
          getc(afile,"%d", &number);
          while (feof(afile)==0)
          {
             printf("%d\n", number);
             putc(anotherfile,"%d\n", number);
             getc(afile,"%d", &number);
          }
          /* do not pass a NULL pointer to fclose() */
          fclose(afile);
       }
       return 0;
    }

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Mindlessly replacing fprintf with putc and fscanf with getc is quite idiotic. The functions are quite different. Do you understand what putc and getc do? If not, refer to whatever reference you're studying from.

  5. #5
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    the below is still throwing errors.

    Code:
    
    #include <stdio.h>
    
    int main()
    {
       FILE *afile, *anotherfile;
       int number;
       int putc( char ch, FILE *fptr );
       int getc( FILE *fptr );
    
       afile=fopen("fun-with-numbers.txt","r");
       anotherfile=fopen("more-fun-with-numbers.txt","w");
    
       if (afile==NULL)
       {
          printf("Could not open file for reading.\n");
       }
       else
       {
          fprintf(stdout,"Success opening file.\n\n");
          getc(afile,"%d", &number);
          while (feof(afile)==0)
          {
             printf("%d\n", number);
             putc(anotherfile,"%d\n", number);
             getc(afile,"%d", &number);
          }
          /* do not pass a NULL pointer to fclose() */
          fclose(afile);
       }
       return 0;
    }

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You don't want to put the function prototypes in your code, they're standard functions, they are prototyped in stdio.h

    putc takes a char and a FILE *.
    getc takes a FILE *.

    Yet int your code, you are treating putc and getc still like fprintf and fscanf.

  7. #7
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    Will you please show me an example on how to incorporate getc and putc. All I can think of is how i presented it, similar to scanf and printf. I am referring to a text that dosen't show a good example, and need some help figuring this out.

    thanks,

  8. #8
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Well, getc reads in one character a time:
    Code:
    int ch;
    ch =  getc(input_filehandle);
    And putc writes out one character a time:
    Code:
    putc(ch, output_filehandle);
    Both functions return EOF on error.

    So, in the original code, you are using fscanf and fprintf to read an integer value at a time, and write it out. In your new code, you will be using fgetc and fputc to read a character at a time, and write it out.

  9. #9
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    hmm, check what I did. Is this any closer?

    Code:
    #include <stdio.h>
    
    int main()
    {
       FILE *afile, *anotherfile;
       int number;
       int ch;
       ch =  getc(input_filehandle);
       putc(ch, output_filehandle);
    
       afile=fopen("fun-with-numbers.txt","r");
       anotherfile=fopen("more-fun-with-numbers.txt","w");
    
       if (afile==NULL)
       {
          printf("Could not open file for reading.\n");
       }
       else
       {
          fprintf(stdout,"Success opening file.\n\n");
          getc(afile,"%d", &number);
          while (feof(afile)==0)
          {
             printf("%d\n", number);
             putc(anotherfile,"%d\n", number);
             getc(afile,"%d", &number);
          }
          /* do not pass a NULL pointer to fclose() */
          fclose(afile);
       }
       return 0;
    }
    thank you

  10. #10
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    why don't u look up the syntax? It's way off. I'll give you a hint: google.com, search for getc
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  11. #11
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You are just parroting whatever examples I give and inserting them into your code, without actually understand what you're doing, or what the code is doing.

    I'm not going to spoonfeed the solution to you, think for yourself.

  12. #12
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    Well my fault, I assumed this was a help message board, one that helps one in need. Not a board where people say "nope, thats wrong" and give no help or examples, and then proceed to tell me to do a google search. I know im wrong, thats why im posting here. The last I would need to for someone to repeat that I am wrong, and then provide no assisatance. Excellent.

  13. #13
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    HELP ME HELP YOU...famous line made by....me. It means, you need to do some work for yourself, such as a simple google search to find your answer. You will find when things get complex, most people won't know how to help you, therefore, you need to learn how to find answers yourself. Don't get mad that people won't write the program for you, we say it, for you to play it.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  14. #14
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Perhaps you should have read the forum guidelines including questions regarding homework, then.

  15. #15
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by otchster
    Well my fault, I assumed this was a help message board, one that helps one in need. Not a board where people say "nope, thats wrong" and give no help or examples, and then proceed to tell me to do a google search. I know im wrong, thats why im posting here. The last I would need to for someone to repeat that I am wrong, and then provide no assisatance. Excellent.
    hate it when they pick on me and not try and help me! here let me give you a helping hand
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed