Write a program that reads from a text file named "messages.txt" line by line (until end-of-file,EOF) and echoes the messages to the display.
Have the program recognize and implement the following command-line arguments:
多谢,实在不知道怎么写
Write a program that reads from a text file named "messages.txt" line by line (until end-of-file,EOF) and echoes the messages to the display.
Have the program recognize and implement the following command-line arguments:
多谢,实在不知道怎么写
英文看不大明白,这个命令行参数是cmd窗口中输入的命令行参数么?
#include <stdio.h>
int main(int args,char *argv[])
{
FILE *fp = NULL;
int i=0,n=0,m=0;
char buf[1000];
char word[100][20] = {0};
if(args < 2)
{
printf("参数不足");
return 0;
}
fp = fopen(argv[1],"r");
if(fp == NULL)
{
printf("文件打开失败.");
return 0;
}
while(fgets(buf,1000,fp) != NULL)
{
for(i=0;i<100;i++)
memset(word[i],0,20);
i=0;
n=0;
m=0;
if(args == 2)
printf("%s\n",buf);
else
{
if(argv[2][1] == 'u')
{
while(buf[i] != '\0')
{
if(buf[i]>='a' && buf[i] <='z')
printf("%c",buf[i] - 32);
else
printf("%c",buf[i]);
i++;
}
}
else if(argv[2][1] == 'l')
{
while(buf[i] != '\0')
{
if(buf[i]>='A' && buf[i] <='Z')
printf("%c",buf[i] + 32);
else
printf("%c",buf[i]);
i++;
}
}
else if(argv[2][1] == 'r')
{
while(buf[i] != '\0')
{
if(buf[i] == '\n')
{
i++;
continue;
}
if(buf[i] == ' ')
{
if(m!=0)
n++;
m=0;
}
else
word[n][m++] = buf[i];
i++;
}
if(m!=0)
n++;
for(i=n-1;i>=0;i--)
printf("%s ",word[i]);
}
}
printf("\n");
}
fclose(fp);
return 0;
}