Thread: file use

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    Question file use

    Ive been following the examples and tutorials in K&R the c programming language yet i have no idea how to use files, for example The code below shows an attempt at counting the amount of tabs in a text file however, once compiled $./program text.txt doesn't do anything. Any help is appreciated.

    Code:
    #include <stdio.h>
    
    int main (int argc, char *argv[])
    {
    int c, nt;
    	nt = 0;
    	while ((c = getchar()) !=EOF)
    		   if (c == '\t')
    		   nt++;
    
       printf("%d tabs", nt);
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    That program counts the tabs you enter from the keyboard. To use it with a file, do this:

    ./program < input.txt

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You haven't opened any files. Open a file for reading like this:
    Code:
    FILE *fstRO = fopen(argv[1], "r");
    if (fstRO == NULL) {perror("Couldn't open"); exit -1;}
    and remember to fclose the stream (fstRO) later. In this case, the file will be whatever you use as the first argument to the program when you start it. The second line is to make sure the file has actually been opened; it requires stdlib.h . Then, you want to read character-by-character thru the file and add up the tabs -- except getchar won't do this (unless you pipe fstRO into stdin) because getchar reads from stdin. So instead use getc:
    Code:
    while ((getc(fstRO)) != EOF) if (c == '\t') nt++;
    So here's your code with corrections:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (int argc, char *argv[]) {
            int c, nt=0;
            FILE *fstRO = fopen(argv[1], "r");
            if (fstRO == NULL) {perror("Couldn't open"); exit -1;} 
    
            while ((getc(fstRO)) != EOF) if (c == '\t') nt++;
            fclose(fstRO);
            printf("&#37;d tabs\n", nt);
            return 0;
    }
    Unfortunately, there is one last problem that you'll have to test yourself: Most likely, "tabs" as a single character don't exist in your text files at all (even if they got there because you pressed "tab" somewhere sometime). They're just a number of inserted spaces. So all the output is ever likely to be is:

    0 tabs

    Oh well...
    Last edited by MK27; 09-14-2008 at 08:24 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    ((getc(fstRO)) != EOF) if (c == '\t')
    what magical connection have you made between changing c and calling getc?
    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

  5. #5
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    Question

    Great! thanks! But there is something odd If we look at one method which i think used the original code gives

    Code:
    vivi@debian:~/c$ ./robwhit < test.c
    7 tabsvivi@debian:~/c$
    whereas another method brings up no tabs on exactly the same file.

    Code:
    vivi@debian:~/c$ ./mk27 test.c
    0 tabs
    Not sure why this is.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Why won't tabs exist at the file? Tab is one character. Every 'X' is one charcter (1 byte). The program displaying the information of a text "translates" a tab character into spaces.

    It is because MK27 had a mistake as pointed out by Vart. Obviously he wanted to write:
    Code:
    ((c = getc(fstRO)) != EOF) if (c == '\t')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM