我有一個程序,應該這樣做:讀取線,並返回行包含字
- 打開文件
- 讀取字符每行的字符
- 打印在另一個文件含有字
線I必須尊重此條件:與文件描述符方法打開的文件,由字符讀取的字符,並使用<string.h>
功能。 我發現了其他類似的問題,但真的不同..和fopen
用於訪問文件。
這是我的代碼(由主在一個週期內調用的函數):
#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';
我覺得有一個結構性或概念性的錯誤,但我找不到它。
請學會像程序員一樣思考,避免像「不起作用」這樣的詞彙。它究竟做了什麼與你期望的不同? –
'if(!lineEnded){ counter + = 1; line [counter] ='\ n';} char * local_line; strncpy(local_line,line,counter + 1);'是混亂的。添加\ n錯誤的地方,不保證null字符終止,不測試'local_line'是否爲'NULL' – chux
@JonathanWood,謝謝,我會做的! 程序必須逐行讀取「in_file」(逐字符逐行讀取)。然後檢查該行是否包含「單詞」。如果包含它,則該行被寫入'out_file'中,如果不是,則不執行任何操作並從'in_file'中讀取下一行。 函數'readLine'讀取1行,主要使用該函數讀取多行。 – Fede