1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Linux wc command

  1. Oct 19, 2009 #1
    1. The problem statement, all variables and given/known data
    Emulating the wc command in linux


    Output suppose to be like this :
    [root@localhost home]# wc
    This is a test
    1 4 15
    [root@localhost home]# ./wc1.exe
    This is a test
    1 4 15
    [root@localhost home]# cat File1.dat
    This is a test
    [root@localhost home]# wc < File1.dat
    1 4 15
    [root@localhost home]# ./wc1.exe < File1.dat
    1 4 15
    [root@localhost home]#


    2. Relevant equations

    null

    3. The attempt at a solution


    Code (Text):




    #include <stdio.h>
    #define MaxLine 200

    char Line[MaxLine]; //one line from the file

    int NChars = 0; //number of characters seen so far
    int NWords = 0; //number of words seen so far
    int NLines = 0; //number of lines seen so far
    int LineLength; //length of the current line
    int fromfile;
    FILE *fp1;


    PrintLine() //for debugging purposes only

    {

    int I;

    for(I=0; I < LineLength; I++) printf("%c",Line[I]);
    printf("\n");
    }

    int WordCount() //count words


    { int I,NBlanks =0;
    for (I=0;I < LineLength; I++)

    if (Line[I]== ' ') NBlanks++;

    if(LineLength > 1) return NBlanks+1;
    else return 0;
    }

    int ReadLine() //from user input



    {
    char C; int I;

    //printf("please input something: ");
    //scanf("%c",&C);

    if (scanf("%c",&C) == -1) return 0;
    Line[0]= C;
    if (C =='\n') return 1;
    for (I=1;;I++) {
    scanf("%c",&C);
    Line[I]=C;
    if (C=='\n') return I+1;
    }
    }


    int ReadLine2() //from file


    {
    char C; int I;



    if (fscanf(fp1,"%c",&C) == -1) return 0;
    Line[0]= C;
    if (C =='\n') return 1;
    for (I=1;;I++) {
    scanf("%c",&C);
    Line[I]=C;
    if (C=='\n') return I+1;
    }
    }


    UpdateCounts()
    {
    NChars += LineLength;
    NWords += WordCount();
    NLines++;
    }

    main()

    {
    printresult();


    }

    int printresult() {
    while(1) {
    LineLength = ReadLine();
    if(LineLength ==0) break;
    UpdateCounts();
    }
    printf("%7d%8d%7d\n",NLines,NWords,NChars);



    if(!(fp1 = fopen("File1.DAT", "r")))
    {
    printf("Error opening FILE1.DAT for reading");

    while((fromfile =fgetc(fp1)) !=EOF)
    {
    fromfile = ReadLine2();
    if(fromfile ==0) break;
    UpdateCounts();
    }
    printf("%3d%3d%3d\n",NLines,NWords,NChars);
    fclose(fp1);
    }
    }

     

    but my output turned out like this :

    [root@localhost home]# wc
    This is a test
    1 4 15
    [root@localhost home]# ./wc1.exe
    This is a test
    1 4 15
    [root@localhost home]# cat File1.dat
    This is a test
    [root@localhost home]# wc < File1.dat
    1 4 15
    [root@localhost home]# ./wc1.exe <File1.dat
    1 4 15
    Segmentation fault


    How do I get the .dat loop to run? =(

    thank you.
     
    Last edited: Oct 19, 2009
  2. jcsd
  3. Oct 19, 2009 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    Why are you trying to open File1.dat in your program anyway?
    Isn't the whole idea of using the < operator that you can read the file from stdin?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Linux wc command
  1. Linux listing (Replies: 7)

  2. Linux question (Replies: 1)

Loading...