gpt4 book ai didi

c - 系统调用 : Outputing file and pausing every 20 lines

转载 作者:太空宇宙 更新时间:2023-11-04 04:45:18 24 4
gpt4 key购买 nike

因此,我正在读取文件并使用系统调用将文件内容输出到控制台。我还想在输出中每遇到 20 行就添加一个空格。这是我遇到麻烦的地方,因为尽管有以下几行代码,但整个文件的显示都没有空格

     //Write file contenst
while( (nReadFile = read(nOpenFile, buffer, 1) != 0))
{
write(1, &nLineCount, sizeof(nLineCount));

if(nLineCount == 20)
{
write(1, "\n", 2);
nLineCount = 0;
}

if(nReadFile = write(1, buffer, nReadFile) == '\n')
{
nLineCount++;
}

}

这是整个程序(不包括包含库的 .h 文件)

  #include"mymore.h"

int main(int argCount, char *argv[])
{

struct termios initial_settings, new_settings;

tcgetattr(fileno(stdin), &initial_settings);
new_settings=initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
if (tcsetattr(fileno(stdin), TCSANOW, &new_settings)!=0)
{
fprintf(stderr, "could not set attributes\n");
}

int nLineCount = 0;

int nCheckFile = 0;
int nFileCountCounter = 1; //first arguement interested in is argv[1]
int nOpenFile = 0;
int nReadFile = 0;
int nCount = 0;
char *cData;
char buffer[0];

//check that arguements have been provided
if( argCount < 2)
{
write(1, "There needs to be at least one file provided! \n", 50);
return 1;
}

do
{
printf("%d", argCount);
//check if file exists
nCheckFile = access(argv[nFileCountCounter], F_OK);

if(nCheckFile != 0) //if file does not exist
{
write(1, "The file ", 10);
write(1, argv[nFileCountCounter], strlen(argv[1]));
write(1," does not exist! \n", 25);
return 1;
}
else //file does exist
{
write(1, "Opening ", 10);
write(1, argv[nFileCountCounter], strlen(argv[1]));
write(1, "\n", 2);
}

//open the file
nOpenFile = open(argv[nFileCountCounter], O_RDONLY);

if(nOpenFile < 0)
{
write(1, "Failed to open file ", 10);
write(1, argv[nFileCountCounter], strlen(argv[nFileCountCounter]));
write(1, "\n", 2);
return 1;
}

//read file
cData = (char *) malloc(100 * sizeof(char));



cData[nReadFile] = '\0';//append null terminator

//find length of source file
while(cData[nCount] != 0)
{
nCount++;
}

//Write file contenst
while( (nReadFile = read(nOpenFile, buffer, 1) != 0))
{
write(1, &nLineCount, sizeof(nLineCount));

if(nLineCount == 20)
{
write(1, "\n", 2);
nLineCount = 0;
}

if(nReadFile = write(1, buffer, nReadFile) == '\n')
{
nLineCount++;
}

}

cout << nLineCount << endl;
//close file
close(nOpenFile);

//Increment to next file
nFileCountCounter++;

}while(nFileCountCounter < argCount);//while there are still arguements

tcsetattr(fileno(stdin), TCSANOW, &initial_settings);
return 0;
}

这实际上是我第一次使用系统调用,我想我注意到的一件事是 write 命令在任何 c 代码之前执行?

有什么想法吗?谢谢

最佳答案

你的代码中有很多错误:

  1. char buffer[0];应该是 char buffer[1];

    char buffer[0];定义一个 buffer可以存储字符,我想这不是你想要的。

  2. write(1, "There needs to be at least one file provided! \n", 50);应该是

    char *msg = "There needs to be at least one file provided! \n";
    write(1, msg, strlen(msg));

    消息超过50条,有几个类似的错误,可以类似修复。

  3. while( (nReadFile = read(nOpenFile, buffer, 1) != 0))应该是

    while( (nReadFile = read(nOpenFile, buffer, 1)) != 0)

    在 C 中,运算符 !=优先级高于 = , 所以 nReadFile = read(nOpenFile, buffer, 1) != 0测量 nReadFile = (read(nOpenFile, buffer, 1) != 0) .

  4. if(nReadFile = write(1, buffer, nReadFile) == '\n')

    如果你想检查当前字符是否是换行符,你应该像if (buffer[0] == '\n')这样的东西.

  5. cout << nLineCount << endl;

    这是 C++,不是 C。

关于c - 系统调用 : Outputing file and pausing every 20 lines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21715269/

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