2015-11-06 61 views
0

我有一个程序,应该这样做:读取线,并返回行包含字

  1. 打开文件
  2. 读取字符每行的字符
  3. 打印在另一个文件含有字

线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'; 

我觉得有一个结构性或概念性的错误,但我找不到它。

+1

请学会像程序员一样思考,避免像“不起作用”这样的词汇。它究竟做了什么与你期望的不同? –

+0

'if(!lineEnded){ counter + = 1; line [counter] ='\ n';} char * local_line; strncpy(local_line,line,counter + 1);'是混乱的。添加\ n错误的地方,不保证null字符终止,不测试'local_line'是否为'NULL' – chux

+0

@JonathanWood,谢谢,我会做的! 程序必须逐行读取“in_file”(逐字符逐行读取)。然后检查该行是否包含“单词”。如果包含它,则该行被写入'out_file'中,如果不是,则不执行任何操作并从'in_file'中读取下一行。 函数'readLine'读取1行,主要使用该函数读取多行。 – Fede

回答

1
char* local_line; 
strncpy(local_line,line,counter+1); 

在此使用strncpy之前,你需要分配内存以local_line使用malloc或类似的功能。

+0

好的,谢谢。我在第三个解决方案中尝试这个。但不起作用... – Fede

+1

@Fede:再次,“不起作用”没有帮助。请学会像程序员一样思考。 –

+0

@JonathanWood对不起,好的,对不起,但它写在我的第一篇文章中,这个解决方案有问题。如果我使用malloc这行'local_line [counter + 1] ='\ n';'似乎被忽略了。并且在函数结束时,两个不同的'free'(它的执行取决于函数的输出)会导致内存损坏或分段错误错误。 – Fede