Thread: sob.. until now my program wont run...

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

    sob.. until now my program wont run...

    This is my program before, but my teacher ask me to recode the program.. its required to use fgetc(); for character input, but i think its wrong... and I have ong error, its so confusing.. HELP ME!!

    I wish someone will help me again..

    Code:
    #include<stdio.h>
    void main(void)
    {
    FILE *inputf;
    char filename[40];
    int c;
    
    printf("Input name of the text file");
    fgetc(filename,10,stdin);
    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);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fgets used to input strings
    fgetc - used for input single character
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    23
    but how it will accept single character?? i dont get the idea of my teacher, maybe she want some alternative code for scanf?? and she choose to use fgetc(); but i dont know how to apply it on their.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It will never accept a single character--it wasn't designed for it.
    What you need to do is use fgets if you want to pass an array. And the size shouldn't be "10", it should be sizeof(filename).
    If you insist on using fgetc, you need to use a loop to read the input, which is much more complicated and can be avoided.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    fgets() is just fgetc() in a loop.

    Oh, and main returns int, not void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  4. Replies: 3
    Last Post: 07-11-2005, 03:07 AM
  5. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM