Beginners C Programming Challenge

This is a discussion on Beginners C Programming Challenge within the C Programming forums, part of the General Programming Boards category; Hello, I have been reading the tutorial for C on this site and I started to work on the first ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    Beginners C Programming Challenge

    Hello,
    I have been reading the tutorial for C on this site and I started to work on the first Help Free C programming Challenge(for Beginners) which asks to write a program that takes a file as an argument and counts the total # of lines. I compiled my code on Code::Blocks Mingw compiler and there are many errors. Perhaps someone give suggestions how to get this right. Thanks
    UCnLA

    Code is as follows:

    Code:
     
     #include <stdlib.h>  /* Program to count the total # of lines in a file and print result */
    #include <stdio.h>
    
    int line_count=0;     /* variable for total lines in a file*/
    char FILE*fp;
    
    int main(int argc, char* argv[])
    {
        if(argc !=  2)
        {
             /* We print argv[0], assuming it is the name of the file*/
            printf("usage: %s filename", argv[0]);
        }
        else
        {
            /* We assume argv[1] is a filename to open*/
            char FILE*fp = fopen(argv[1], "r");
    
            /* fopen returns 0, the NULL pointer, on failure */
            if ( fp == 0 )
            {
                printf( "Could not open file\n" );
            }
            else
            {
                char x;     /* read one character at a time from file,tests for newline, counts total lines, stopping at
                            (EOF) end of the file).  */
                while  ( ( x = fgetc( *fp ) ) != EOF )
                {
                    if(x = '\n')
                    {
                        line_count++;
                    }
                }
                    line_count++;  /* Counts the final line of the file. */
                    }
            printf("There are %d" line_count,"Total Lines in the file known as %s" argv[0]);
            fclose( FILE*fp );
                }
                return(0);
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    This one?
    Code:
    char FILE*fp; // remove highlighted
    Code:
    char FILE*fp = fopen(argv[1], "r"); // remove highlighted
    Code:
    fclose( FILE*fp ); // remove highlighted
    Code:
    char x; // should be an int -- see FAQ
    Code:
    while  ( ( x = fgetc( *fp ) ) != EOF ) // remove highlighted
    Code:
    if(x = '\n') // change to ==
    Code:
    printf("There are &#37;d" line_count,"Total Lines in the file known as %s" argv[0]);
    Change to:
    Code:
    printf("There are %d Total Lines in the file known as %s", line_count, argv[0]);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    A variable cannot have two types. It can only have one type.
    And I think you need training with pointers, listen to errors or warnings, as well. If a function expects FILE*, you cannot pass FILE.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners C Programming Challenge
    By UCnLA in forum C Programming
    Replies: 23
    Last Post: 04-01-2008, 07:46 PM
  2. Programming Challenge (for my school)
    By Ezerhorden in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2006, 05:56 AM
  3. for beginner's or anyone interested
    By Iconoklast in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-25-2004, 01:45 PM
  4. Requesting a challenge
    By RealityFusion in forum C++ Programming
    Replies: 8
    Last Post: 08-18-2003, 08:24 PM
  5. What is a good beginners' C++ book?
    By GrNxxDaY in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2002, 09:50 AM

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