Emulating Linux WC Command: Counting Words, Chars & Lines

  • Thread starter Thread starter pokkie
  • Start date Start date
  • Tags Tags
    Linux
Click For Summary
SUMMARY

The forum discussion centers on emulating the Linux 'wc' command using a custom C program named 'wc1.exe'. The user successfully counts words, characters, and lines from standard input and a file named 'File1.dat'. However, the program encounters a segmentation fault when attempting to read from 'File1.dat' after opening it in the code. The issue arises from a misunderstanding of file handling in C, particularly regarding the use of standard input versus file pointers.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with file I/O operations in C
  • Knowledge of standard input and output handling
  • Basic concepts of memory management and segmentation faults
NEXT STEPS
  • Study C file I/O functions, specifically fopen, fclose, and fscanf
  • Learn about handling standard input in C using stdin
  • Investigate common causes of segmentation faults in C programming
  • Explore debugging techniques for C programs, including using gdb
USEFUL FOR

C programmers, software developers working with Linux command-line tools, and anyone interested in understanding file handling and input/output operations in C.

pokkie
Messages
1
Reaction score
0

Homework Statement


Emulating the wc command in linuxOutput 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]#

Homework Equations



null

The Attempt at a Solution

Code:
#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 faultHow do I get the .dat loop to run? =(

thank you.
 
Last edited:
Physics news on Phys.org
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?
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K