file input

This is a discussion on file input within the C Programming forums, part of the General Programming Boards category; hello I am trying to read a file into an array of type char. The file is just a basic ...

  1. #1
    gladen
    Guest

    file input

    hello
    I am trying to read a file into an array of type char. The file is just a basic document, so it contains whitespaces and such. The whitepaces should not be copied to the array. What function is the best for this, and how do I use it? I looked at the gnu docs but the are too complex for me.

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up Try this out...

    Hi there,
    Try this code out...and get back to me if u got some problem...


    call the command name delsp.c

    #include<stdio.h>

    void main(int argc, char *argv[])
    {
    FILE *fp;
    char buffer[10000],ch;
    int i;

    if(argc!=2)
    {
    printf("\nFile Not Specified. \nUsage delsp <filename>");
    exit(0);
    }
    fp=fopen(argv[1],"r");
    if(fp==NULL)
    {
    printf("\nUnable to Open Input File. Make sure it exists");
    exit(0);
    }
    i=0;
    while((ch=fgetc(fp))!=EOF)
    {
    if(ch!=' ')
    {
    buffer[i]=ch;
    i++;
    }
    }
    fclose(fp);
    }
    Help everyone you can

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Try this code out...and get back to me if u got some problem...
    I have a problem, several actually but the second is only nitpicking.

    >void main(int argc, char *argv[])
    I can describe this with one word. Wrong.

    >exit(0);
    Yes this is nitpicking, but abruptly terminating after an error is only good for student code. If you did this in a real application without saving data then users would be just a little miffed at losing hours of work because the program couldn't open a file.

    You also use the exit function without including stdlib.h.

    >while((ch=fgetc(fp))!=EOF)
    ch is declared as a char, this can break since EOF is an integer value that usually can't be held by a char variable.

    Personally, I would have structured it differently to create a better flow. The way your program is (once fixed), it seems like you expect it to fail immediately while a program should expect to succeed but still take appropriate measures in the event that it doesn't. Here's how I modified it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXBUF 10000
    
    int main(int argc, char *argv[])
    {
      FILE *fp;
      char buffer[MAXBUF];
      int i = 0, ch;
      if(argc < 2) {
        fp = fopen(argv[1],"r");
        if(fp != NULL ) {
          while((ch=fgetc(fp))!=EOF && i < MAXBUF) {
            if(ch!=' ') {
              buffer[i]=(char)ch;
              i++;
            }
          }
          fclose(fp);
        }
        else
          fprintf(stderr, "\nUnable to Open Input File. Make sure it exists");
      }
      else
        fprintf(stderr, "\nFile Not Specified.\nUsage <programname> <filename>");
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    gladen
    Guest
    Thank you both for your help. I have one question, though: Why did you declare ch as an int instead of a char?

  5. #5
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by gladen
    Why did you declare ch as an int instead of a char?
    Most likely because fgetc() returns an int, not a char ;)
    Jason Deckard

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I have one question, though: Why did you declare ch as an int
    > instead of a char?

    Prelude already gave you the answer, see:

    >> ch is declared as a char, this can break since EOF is an integer
    >> value that usually can't be held by a char variable.

    Basicly, "EOF" that you're checking for, it is not a simple letter or number that is within the bounds of a 'char'.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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