Help out this newcomer

This is a discussion on Help out this newcomer within the C Programming forums, part of the General Programming Boards category; hi everyone out there how r u this is my first time.......coming to the point I wanna ask that I've ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    Question Help out this newcomer

    hi everyone out there
    how r u

    this is my first time.......coming to the point I wanna ask that I've installed turboc 3.0 and I'm doing file handling this time.....but none of the programs is running.
    It always shows me the error "Cannot open the file"

    I've tried it out on visualc++ 6.0 and it worked there but I could not find where was the mistake.

    So please kindly help me out so that I could be able to work with turboc.

    thanx in advance

    I'll be glad to u



    here's the code



    #include "stdio.h"

    void main()
    {
    FILE *fp;
    char ch;
    int nol=0,noc=0,not=0,nob=0;
    fp=fopen("TRY1.C","r");
    if(fp==NULL)
    {
    puts("\nCannot Open File ");
    exit();
    }

    while(1)
    {
    ch=fgetc(fp);

    if(ch==EOF)
    break;

    noc++;
    if(ch==' ')
    nob++;

    if(ch=='\n')
    nol++;

    if(ch=='\t')
    not++;
    }
    fclose(fp);

    printf("\nNo. of Characters = %d",noc);
    printf("\nNo. of blanks = %d",nob);
    printf("\nNo. of tabs = ",not);
    printf("\nNumber of lines = %d",nol);
    }
    Last edited by a_learner; 10-05-2001 at 12:59 PM.

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    #include "stdio.h"

    Should be #include <stdio.h>
    A standard header file that comes with the compiler should be surrounded by <> and a header file you write yourself should be surrounded by "".

    char ch;

    This should be int, not char. You use this variable to check for EOF, and EOF has a higher value than char can handle.

    while(1)
    while 1 is equal to 0? That's not ever going to happen so you won't ever enter this loop.

    exit();
    Won't work because exit has to take a parameter and is defined in <stdlib.h>
    Try exit(1); and include <stdlib.h> in your headers
    Last edited by *pointer; 10-05-2001 at 01:33 PM.
    pointer = NULL

  3. #3
    Unregistered
    Guest
    you havent the header file for the exit function which is <stdlib.h> and you have no parameter for the exit function either, it needs an integer, The prototype is void exit(int return code). You could also improve the while loop by placing the test for EOF inside the while expression ie: while( (ch = fgetc(fp)) != EOF)


    happy coding

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by *pointer

    while(1)
    while 1 is equal to 0? That's not ever going to happen so you won't ever enter this loop.
    Umm, the while() function executes whenever the parameter is NOT zero, so while(1) is an infinite loop, which he wants.

    AND, in any event, his program isn't even reaching these lines of code.

    When you compile this with turbo C, are you executing the program in the directory that try1.c is in? Because the problem seems to be that you are not able to open the input file.

    Try putting the full path in the fopen, like:

    fopen("c:\\files\\try1.c","r");

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,658
    #include "a_header.h"


    More specifically, a header in quotes specifies one that is in the same directory(folder, actually) as the program.



    ITSA
    Socket Library!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > More specifically, a header in quotes specifies one that is in the same directory(folder, actually) as the program
    "" just means that the search for the file starts there. If the .h file isn't in the same place as the .c file, then "" is the same as <>
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    heyyyyyyyy

    It worked out on Visual C++6.0
    everything is running successfully

    well I had some new problems
    I'm trying them out.......if it dosen't solves out then again upppp to u......

    thanx to everyone for helping this newcomer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Newcomer Question
    By anirban in forum Windows Programming
    Replies: 2
    Last Post: 04-18-2007, 01:26 PM
  2. String question, and hello from a newcomer
    By izuael in forum C++ Programming
    Replies: 13
    Last Post: 10-14-2006, 03:16 AM
  3. Help for a newcomer please.
    By gasgastxtpro in forum C++ Programming
    Replies: 15
    Last Post: 08-24-2005, 10:09 AM
  4. Python newcomer learning C
    By caroundw5h in forum C Programming
    Replies: 0
    Last Post: 10-22-2003, 03:11 PM

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