Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

how to read and write to fiel n c

#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>


/*
 * Functions Signatures
 * */



size_t my_strlen(const char * str);
void my_strcat(char * dest, const char * src);
bool start_with(  char* s, char* prefix);
unsigned int num_words(const char *  str);
char * get_chapter_file_name(const char* prefix, const char* s, const char* suffix );



int main(int argc, const char * argv[]) {
    unsigned int total_lines = 0, total_words = 0, total_chars = 0;
    char *buf = NULL;


    size_t buff_size = 0;
    FILE *f_in = fopen(argv[1] ,  "r");
    FILE *file_name = NULL;
    char * chapter = get_chapter_file_name("Moby-","PREFACE
", ".txt" );
    file_name = fopen(chapter, "w");


    if(argc != 2) {
        fprintf(stderr, "
Usage: %s <file-path>
", argv[0]);
        return 1;
    }



    if ( ( !f_in ) || (!file_name) ) {
        printf((const char *) stderr, "Filed to open %s", f_in);
        exit(-1);
    }






    unsigned int n_words=0, num_chars=0, num_lines=0;
    while( getline( &buf, &buff_size, f_in )  != EOF) {

        if (start_with(buf, "CHAPTER")) {
            fclose(file_name);
            total_lines += num_lines;
            total_chars += num_chars;
            total_words += n_words;
            printf("%-30s : %d lines, %d words, %d characters
", chapter, num_lines, n_words, num_chars);
            free(chapter);
            num_lines = 0;
            num_chars = 0;
            n_words = 0;
            chapter = get_chapter_file_name("Moby-", buf, ".txt");
            file_name = fopen(chapter, "w");
            if (!file_name) {
                exit(-1);
            }

        }
        fprintf(file_name,"%s", buf);
        num_chars += my_strlen(buf);
        n_words += num_words(buf);
        num_lines++;

    }
    printf("TOTAL                          : %d lines, %d words, %d characters
", total_lines, total_words, total_chars);
    free(buf);
    fclose(f_in);
    fclose(file_name);
    return 0;


}

/* Find the length of a string */
size_t my_strlen(const char *str) {
    int i = 0;
    while (*str) {
        i++;
        str++;
    }
    return i;
}

/* I did not need this function
 * However I defined it that way */

void my_strcat(char *dest, const char *src) {
    unsigned int len_of_des = my_strlen(dest);
    unsigned int len_of_src = my_strlen(src);

    while (*dest) {
        ++dest;
    }


    while (*src) {
        *dest = *src;
        src++;
        dest++;
    }

    unsigned int go_back = (len_of_des + len_of_src);
    while( go_back != 0 ) go_back--, dest--;

}


/* This function will Catch * char buffer
 * That is coming from the mobybock.txt
 * Will return true if the buffer contains the prefix -> Chapter */
bool start_with(char *s, char *prefix) {
    char *s_ptr = NULL;
    char *prefix_ptr = NULL;

    for (s_ptr = s, prefix_ptr = prefix; *prefix_ptr; s_ptr++, prefix_ptr++) {
        if ( (*s_ptr) != (*prefix_ptr) )
            return false;
    }

    return true;
}

/* Counts number of words inside a string */
unsigned int num_words(const char *str) {
    if (str == NULL)
        return 0;

    bool is_one_space = true;
    int words = 0;

    while (*str != '') {
        if ((*str == ' ') && is_one_space) {
            is_one_space = false;
            ++words;
            ++str;
        } else if ((*str != '
') && (*str != '	')) {
            is_one_space = true;
            ++str;
        } else {
            ++str;
        }

    }

    return words;
}


/* Will return each chapter as a txt file name */
char *get_chapter_file_name(const char *prefix, const char *s, const char *suffix) {
    unsigned int len_s = my_strlen(s);
    unsigned int  len_prefix = my_strlen(prefix);
    unsigned int len_suffix = my_strlen(suffix);

    size_t size = len_prefix + len_s + len_suffix;


    char *arr = malloc(  sizeof(char) *  (size+1)  );
    *arr='';

    unsigned int count_prefix = 0;
    my_strcat(arr,prefix);
    arr[len_prefix+1] = '';



    /* Adding "-" for white spaces */
    char * end_of_s = s;
    end_of_s += len_s - 1;
    unsigned int i= len_prefix;
    for (; *s && (s != end_of_s-1) ; count_prefix++, s++) {

        if ( (*s == ' ') ) {
            // Add '-' if there is a space
            arr[i++] = '-';

        } else {
            arr[i++] = *s ;
            // Else add the char;
        }
    }arr[i]= '';
    my_strcat(arr,suffix);
    arr[i + len_suffix] = '';

    return arr;
}


 
PREVIOUS NEXT
Tagged: #read #write #fiel
ADD COMMENT
Topic
Name
9+7 =