我正在尝试计算我通过文件描述符读取的文件的行数,但我不知道我做错了什么,因为它没有worlk。
这是代码:
fd_openedFile = open(filename, O_RDONLY)
char *miniBuffer[1];
int lineCounter = 0;
while( read(fd_openedFile, miniBuffer, 1) >0) {
if (*miniBuffer[0] == 'n')
lineCounter++;
}
该软件从未输入"如果",我已经测试了很多我认为可以工作的变体,但没有一个有效(这只是对我来说更有意义的变体)。
任何帮助都将不胜感激。
谢谢!
我在下面添加了完整的代码:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
void err_sys(const char* cadena, int continueExecuting) {
perror(cadena);
if (continueExecuting == 0)
exit(1);
}
int main()
{
//vars
char filename[200];
int fd_output = 1;
int fd_openedFile = -1;
int fd_newFile = -1;
//Ask for the file and open it
while (fd_openedFile < 0){
write(fd_output, "Write the filename: ", 20);
scanf("%s", filename);
if ((fd_openedFile = open(filename, O_RDONLY)) < 0)
err_sys("Error opening the original file", 1);
}
//Construct the new file's name
char *partOfOldFilename = strtok(filename, ".");
char newFileName[208], finalPart[8];
strcpy(newFileName, partOfOldFilename);
strcpy(finalPart, "OUT.txt");
strcat(newFileName, finalPart);
//Create the new file
if ((fd_newFile = open(newFileName, O_WRONLY | O_CREAT)) < 0)
err_sys("Error opening the new file", 1);
//Count the number of lines
char miniBuffer[1];
int lineCounter = 0;
while( read(fd_openedFile, &miniBuffer[0], 1) >0) {
write(fd_output, "R", 1); //To debug
if (miniBuffer[0] == 'n') {
lineCounter++;
write(fd_output, "1", 1); //To debug
} else {
write(fd_output, "0", 1); //To debug
write(fd_output, miniBuffer, 1); //To debug
}
}
lseek(fd_openedFile,0,SEEK_SET);
write(fd_output, "=========n", 10); //To debug
//Count the number of chars per line
char* charsPerLine[lineCounter];
lineCounter = 0;
int charCounter = 0;
while( read(fd_openedFile, miniBuffer, 1) >0){
write(fd_output, "C", 1); //To debug
if (miniBuffer[0] == 'n') {
*(charsPerLine[lineCounter]) = charCounter +'0';
lineCounter++;
charCounter = 0;
write(fd_output, "1", 1); //To debug
} else {
write(fd_output, "0", 1); //To debug
write(fd_output, miniBuffer, 1); //To debug
charCounter ++;
}
}
lseek(fd_openedFile,0,SEEK_SET);
write(fd_output, "END", 4); //To debug
//Write a copy of the original file starting each line with the number of chars in it
lineCounter = 0;
int bufSize = 1;
char buffer[bufSize];
//First number write
if (write(fd_newFile,charsPerLine[lineCounter], bufSize)!=bufSize)
err_sys("write_error", 0);
lineCounter++;
while( read(fd_openedFile, buffer, bufSize) >0){
if (write(fd_newFile,buffer, bufSize)!=bufSize)
err_sys("write_error", 0);
if (buffer[0] == 'n') {
if (write(fd_newFile,charsPerLine[lineCounter], bufSize)!=bufSize)
err_sys("write_error", 0);
lineCounter++;
}
}
//Finish program
if (close(fd_openedFile)!=0) err_sys("error closing original file's file descriptor", 0);
if (close(fd_newFile)!=0) err_sys("error closing new file's file descriptor", 0);
return 0;
}
此代码假定该文件是.txt,并且在每行的末尾都有一个"中断线",并且当前正在开发中。
再次感谢。
您没有为miniBuffer
分配任何内存,这是一个char
指针数组。这不是真正的问题 - 问题是它首先不应该是一个char
指针数组。您只需要它是一个字符数组,如下所示。
char miniBuffer[1];
然后,另一个更改是检查数组的单个元素是否为n
字符。
if (miniBuffer[0] == 'n')
您可能会发现,通过增加数组的大小并使用 strchr
等函数查找字符串中的任何n
,读取更大的块会更有效。您需要存储返回read
金额,以便您可以正确NUL
终止字符串。