character input using fgetc() code???

This is a discussion on character input using fgetc() code??? within the C Programming forums, part of the General Programming Boards category; heres my program and I wonder if I can use fgetc() as alternative for scanf() to read an input character... ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    23

    character input using fgetc() code???

    heres my program and I wonder if I can use fgetc() as alternative for scanf() to read an input character...

    Code:
    #include<stdio.h>
    void main(void)
    {
    FILE *inputf;
    char filename[40];
    int c;
    
    printf("Input name of the text file");
    fgetc(filename);
    
    if (inputf = fopen(filename, "r") == NULL)
    {
    printf("ERROR: %s cannot be opened.\n", filename);
    exit(1);
    }
    while ((c = fgetc(inputf)) != EOF)
    fputc(c, stdout);
    fclose(inputf);
    }
    I think the way I used fgetc() their is technically wrong.. so anyone correct me please...??

    THANKS!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,960
    fgetc() takes a FILE* as an argument and returns an int (the character), so fgetc(filename) is definitely wrong. Perhaps you want to use fgets() instead?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    23
    yes its a requirements to use fgetc() for character input.. but I dont know how to put it correctly on the program..
    thanks for reply..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,960
    yes its a requirements to use fgetc() for character input
    For this part of the program you are reading the filename, so use fgets(). For the part where you are reading from the file use fgetc().

    If you insist on using fgetc() to read the filename, then you need a while loop to iterate over the filename and insert with fgetc(stdin).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  5. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 10:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21