gpt4 book ai didi

c 编程 , 字数统计

转载 作者:行者123 更新时间:2023-11-30 21:43:44 24 4
gpt4 key购买 nike

Write a C program named “mywc.c” simulating the “wc” command taking some options and text file as arguments.
If no option is given, mywc outputs the number of lines, words and characters in the given file as well as the file name separated by a blank space.
When the –l option is used, mywc can output the number of lines;
When the –w option is used, mywc can output the number of words;
When the –c option is used, mywc can output the number of characters.
If the given file doesn’t exist, it gives an error message with program name, text file name and the causal separated by colon. The program should also meet the following requirements.
(1) Use structure to store the number of lines, words, and characters.
(2) Characters only include letters and digits.
(3) The order of options does not matter.
(4) When take the screenshot, always show the text first.
The output could be like this:

(omitted)

我执行了这段代码,但没有工作

#include <stdio.h>
#include <string.h>

int main() {
FILE *fp;
char buff[255];
char command[255];
int c=0,i,j=0;
int line=0;
int word=0;
char ch;
char fileName[255];
printf("*********************************************\n");
printf("Enter command (Example -c input.txt)\n");
printf("-c for Number of character in file \n");
printf("-w for Number of words in file \n");
printf("-l for Number of lines in file \n");
printf("*********************************************\n");
scanf("%s",command);
//Extracting file name
if(command[2]==' ') {
if(command[1]=='l' || command[1]=='w' || command[1]=='c') {
while(command[i]!='\0') {
if(i>2) {
fileName[j]=command[i];
i++;
}
fileName[i]='\0';
}
printf("*********************************************\n");
printf("File Name You have provided is:%s\n",fileName);
printf("*********************************************\n");
fp = fopen(fileName, "r");
if(fp) {
while(!feof(fp )) {
memset(buff, '\0', sizeof( buff) );
fgets(buff, 255, (FILE*)fp);
for(i = 0; buff[ i ]; i++)
{
if (buff[i] == '\n')
line ++,word++;
else if (buff[i] == ' ')
word ++;
else
c++;
}
}
fclose(fp);
if(command[0]=='-' && command[1]=='w')
printf("Number of words in file: %i\n", word);
else if(command[0]=='-' && command[1]=='c')
printf("Number of characters in file: %i\n",(c-2));
else if(command[0]=='-' && command[1]=='l')
printf("Number of lines in file: %i \n", line);
else {
printf("Number of words in file: %i\n", word);
printf("Number of characters in file: %i\n",(c-2));
printf("Number of lines in file: %i \n", line);
}
printf("*********************************************\n");
} else
printf("File not found");
return;
}

每次我使用菜单选项和文本文件运行此代码时,我总是会收到段错误(核心转储) 错误。我怎样才能避免这种情况?

最佳答案

使用fgets以便在一行中读取所有字符串。这里只读取了 1 个字符串。

或者一步步获取命令。然后寻找操作。

注意:初始化i

问题

还有另一件事 scanf 读取,直到找到换行符/空格/制表符。 命令中未读取 SO 文件名。

while( fgets(buffer, BUFFERSIZE , stdin) ) /* break with ^D or ^Z */
{
//process buffer
}

哪里出了问题?

scanf("%s",command); //use fgets here you are not reading everything here

//Extracting file name
if(command[2]==' ') {

if(command[1]=='l'||command[1]=='w'||command[1]=='c') {
while(command[i]!='\0')
{
if(i>2) {
fileName[j]=command[i];
i++;
}
fileName[i]='\0';
}

为了了解出了什么问题,您只需在阅读命令后添加一条打印语句,然后打印它并注释掉其余的代码。

关于c 编程 , 字数统计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34047050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com