Fede
Fede

Reputation: 1

Read lines from a file, and returns lines containing a word

I have a program that should do this:

  1. Open a file
  2. Read every line character by character
  3. Print in another file the lines containing a word

I have to respect this conditions: open files with file descriptors method, read character by character and use the <string.h> functions. I've found other similar question but are really different.. and fopen is used to access files.

This is my code (a function called by the main in a cycle):

#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);
}

This is the part of code with problems with related execution errors (you can find it in the function).

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

I think there is a structural or conceptual error, but I can't find it.

Upvotes: 0

Views: 91

Answers (1)

ameyCU
ameyCU

Reputation: 16607

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

In this before using strncpy you need to allocate memory to local_line using malloc or similar function.

Upvotes: 1

Related Questions