Emulating Linux WC Command: Counting Words, Chars & Lines

  • Thread starter pokkie
  • Start date
  • Tags
    Linux
In summary, the conversation discusses the implementation of the wc command in linux and the use of the < operator to read a file from stdin. The code provided is meant to count the number of lines, words, and characters in a given input. However, there is an issue with the loop for reading the .dat file causing a segmentation fault.
  • #1
pokkie
1
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
  • #2
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?
 
  • #3


Your code seems to have some syntax errors and may be causing the segmentation fault. I would suggest debugging your code and fixing any errors before trying to run it again. You can also try using a debugger tool or printing out the values of your variables at different points in the code to see where the error is occurring. Additionally, make sure you are properly closing your file after reading from it.
 

Related to Emulating Linux WC Command: Counting Words, Chars & Lines

1) What is the purpose of the Linux WC command?

The Linux WC (word count) command is used to count the number of words, characters, and lines in a given file or input. It is a useful tool for analyzing text and determining its size or length.

2) How do I use the WC command to count words, characters, and lines in a file?

To use the WC command, simply type "wc" followed by the name of the file you want to analyze. For example, "wc myfile.txt" will display the number of words, characters, and lines in the file named "myfile.txt". You can also use the "-w", "-c", and "-l" options to specify which count you want to see (words, characters, or lines).

3) Can the WC command count words, characters, and lines from multiple files at once?

Yes, you can use the WC command to count words, characters, and lines from multiple files at once by specifying the names of the files after the command. For example, "wc file1.txt file2.txt" will display the total counts for both files combined.

4) What is the difference between the "-w" and "-m" options in the WC command?

The "-w" option counts the number of words in a file, while the "-m" option counts the number of characters. The main difference between the two is that the "-m" option includes spaces and other non-printable characters in its count, while the "-w" option only counts printable characters.

5) Are there any other useful options for the WC command?

Yes, there are several other options that can be used with the WC command, such as "-L" to display the length of the longest line in a file, "-Lw" to display the length of the longest word, and "-Lc" to display the length of the longest line in bytes. There is also a "-help" option that displays a list of all available options and their descriptions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
929
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top