newbie question

This is a discussion on newbie question within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to write a program which just reads from a file and prints the characters on the screen, ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    6

    newbie question

    Hi,
    I'm trying to write a program which just reads from a file and prints the characters on the screen, plz take a look at the code and tell me whats wrong
    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <fcntl.h>
    
    main(int argc, char *argv[])
    {
            int i,j;
            char c;
            FILE *fp;
    
            if(argc < 2)
              fprintf(stderr, "usage a.out <filename>\n");
            fp = fopen(*++argv, O_RDONLY);
            if(fp == NULL)
            {
                    fprintf(stderr, "File cannot be opened\n");
                    exit(1);
            }
            else
            {
                    while ((j = read(fp, &c, 1)) != 1)
                    {
                    //      j = read(fp, &c, 1);
                            if(j <= 0)
                               break;
                            else
                               putchar(c);
                    }
                    fclose(fp);
            }
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Code:
    while ((j = read(fp, &c, 1)) != 1)
    That statement is never going to be true (unless there's nothing in the file). Think about it and read the manual for the read function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 03:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

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