我有一个应该这样做的程序:
我必须遵守这些条件:使用文件描述符方法打开文件,逐个字符地阅读并使用<string.h>
函数。
我发现了其他类似的问题,但确实有所不同...... fopen
用于访问文件。
这是我的代码(在循环中由main调用的函数):
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#define LINELENGTH 2000
int readLine(int in_fd,int out_fd,char** _line,char* word){
/*what the functions return:
-1 end of file,
0 if word not founded
>0 word founded -> return the amount of line characters
*/
//declarations
int counter,lineEnded,fileEnded,readReturn;
char character;
char* line = *_line;
//line acquisition
counter=lineEnded=fileEnded=readReturn=0;
do{
//read
readReturn=read(in_fd,&character,1);
//depends by the read return value:
if(readReturn==-1){ //-error
perror("read error");
exit(EXIT_FAILURE);}
else if(readReturn==0){ //-end of file
if(counter==0) fileEnded=1;
else lineEnded=1;}
else if(character=='\n'){ //-character read is '\n'
line[counter]=character;
lineEnded=1;}
else{ //-character read
line[counter]=character;
counter++;}
}while((counter<LINELENGTH-1) && (!lineEnded) && (!fileEnded));
if(fileEnded) return -1;
//if "line" were filled and then stop reading, so the input
//line probably continue; this "if" force to add
//a '\n' character at the end of line and increase counter
if(!lineEnded){
counter+=1;
line[counter]='\n';}
//copy the line in a new string - 3 NOT WORKING SOLUTIONS
//1st solution: Segmentation Fault
char* local_line;
strncpy(local_line,line,counter+1);
//2nd solution: so i try to use this; but this
//delete the last character to insert '\n'
char* local_line;
local_line = strtok(line,"\n");
local_line[counter-1]='\n';
//3rd solution: seems to work but...
char* local_line = (char*)malloc(sizeof(char)*(counter+1));
local_line = strtok(line,"\n");
local_line[counter+1] = '\n'; //but this line seems to be ignored;
//line written in output file do not contain \n at the end
//search "word" in "local_line"
char* strstrReturn = strstr(local_line,word);
//write line on file represented by out_fd (if word founded)
if(strstrReturn==NULL){
free(local_line); //only with the 3rd solution.. but this line
//causes Memory Corruption after some fuction cycles!
return 0;}
else{
write(out_fd,local_line,counter);
free(local_line); //only with the 3rd solution.. but causes
//Segmentation Fault!
return counter;
}
}
main(int argc,char* argv[]){
//check arguments
if(argc!=3){
printf("syntax: exec fileName wordSearch\n");
exit(EXIT_FAILURE);}
//declarations
int fd_1,fd_2;
int readLineReturn=0;
//int debug;
char* line = (char*)malloc(sizeof(char)*LINELENGTH);
//open file for reading
fd_1 = open(argv[1],O_RDONLY);
if(fd_1<0){
perror("error opening fd_1");
exit(EXIT_FAILURE);}
//open file for writing
fd_2 = open("outFile.txt",O_WRONLY|O_TRUNC|O_CREAT,0664);
if(fd_2<0){
perror("error opening fd_2");
exit(EXIT_FAILURE);}
//line acquisition
int readLineReturn;
do{
readLineReturn = readLine(fd_1,fd_2,&line,argv[2]);
}while(readLineReturn!=-1);
close(fd_2);
close(fd_1);
free(line);
printf("\n");
exit(EXIT_SUCCESS);
}
这是代码中存在相关执行错误问题的部分(您可以在函数中找到它)。
//copy the line in a new string - 3 NOT WORKING solutions
//1st solution: Segmentation Fault
char* local_line;
strncpy(local_line,line,counter+1);
//2nd solution: so i try to use this; but this
//delete the last character to insert '\n'
char* local_line;
local_line = strtok(line,"\n");
local_line[counter-1]='\n';
//3rd solution:
char* local_line = (char*)malloc(sizeof(char)*(counter+1));
local_line = strtok(line,"\n");
local_line[counter+1] = '\n';
我认为存在结构性或概念性错误,但我无法找到它。
答案 0 :(得分:1)
char* local_line;
strncpy(local_line,line,counter+1);
在使用strncpy
之前,您需要使用local_line
或类似功能将内存分配给malloc
。