所以我正在读取文件并使用系统调用将文件内容输出到控制台。另外,我想在输出中添加一个空格,每遇到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;
}
这实际上是我第一次使用系统调用的经历,我认为我注意到的一件事是在任何c代码之前执行了write命令?
有什么想法吗? 感谢
答案 0 :(得分:0)
您的代码中存在大量错误:
char buffer[0];
应为char buffer[1];
char buffer[0];
定义了buffer
,可以存储零字符,我想这不是您想要的。
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.有类似的错误,并且可以类似地修复。
while( (nReadFile = read(nOpenFile, buffer, 1) != 0))
应为
while( (nReadFile = read(nOpenFile, buffer, 1)) != 0)
在C中,运算符!=
的优先级高于=
,因此nReadFile = read(nOpenFile, buffer, 1) != 0
meas nReadFile = (read(nOpenFile, buffer, 1) != 0)
。
if(nReadFile = write(1, buffer, nReadFile) == '\n')
如果你想检查当前字符是否是换行符,你应该像if (buffer[0] == '\n')
那样。
cout << nLineCount << endl;
这是C ++,而不是C。