Thread: All iterations of a string appear on a single line after using strchr

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    5

    All iterations of a string appear on a single line after using strchr

    I want to extract all the words from this file (each word on 1 line)

    mytab24112.txt

    Code:
    apple:$1$$Tnq7a6/C1wwyKyt0V/.BP/
    orange:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1
    banana:$1$$zZQKNjRd94GHyYOwXuStf0
    a:$1$$Ij31LCAysPM23KuPlm1wA/
    a:$6$$ek/ucQg0IM8SQLyD2D66mpoW0vAF26eA0/pqoN95V.F0nZh1IFuENNo0OikacRkDBk5frNqziMYMdVVrQ0o.51
    aah:$1$$bh5o1cAKfH43fc/B1.AjF0
    aah:$6$$jdxDww2LkNSlQPmWe5iDRAoBBT5IXa9241nN2bcm2Aukdrr4iH27Y6dj801MjLFaDQxbDBNxY4jvlXdIemiCY/
    aahed:$1$$zA2Ef92JR9W7kqAf8Km5B0
    aahed:$6$$mYQU7f3NL9Dysccvwv2BAyiCb/gxh9lN1gGnC9sa2uwHGX9VVb7jp3b7u/EZbdObqWrRFgiOLcRn7PEW1cOcz.
    but with this code

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <strings.h>
    
    
    int main(void){
    char buf[1024];
    char **arr1 = NULL;
    char **arr2 = NULL;
    int size1 = 0;
    int size2 = 0;
    FILE * f1, *f2;
    f1 = fopen("mytab24112word.txt", "w+");
    f2 = fopen("mytab24112.txt", "r");
    
    while(fgets(buf, 1024, f1))
        {
            size1++;
            arr1 = realloc(arr1, sizeof(char*) * size1);
            arr1[size1 - 1] = strdup(buf);
        }
    
    
        while(fgets(buf, 1024, f2))
        {
            size2++;
            arr2 = realloc(arr2, sizeof(char*) * size2);
            arr2[size2 - 1] = strdup(buf);
        }
    
    char line[1000]; 
    char * hash2;
    char * del2;
    char word[sizeof(line)];
    
    
    // Read mytab24112.txt line by line
        for(int j = 0; j < size2; j++){
    
    
        strcpy(line, arr2[j]);
       
        // Returns the password text using delimiter ':'
        strcpy(word, line);
        del2 = strchr(word, ':');
        
    
    
        if (del2 != 0)
        *del2 = '\0';
        
    //Puts all iterations of word into f1, which is mytab24112word.txt
        fputs(word,f1);
    
    
    
    
         }// End of "for(int j = 0; j < size2; j++)" loop  
    }//End of main
    but I end up with this.

    mytab24112word.txt

    Code:
    appleorangebananaaaaahaahaahedaahed
    How do I go about achieving this result?

    Desired output for mytab24112word.txt
    Code:
    apple
    orange
    banana
    a
    a
    aah
    aah
    aahed
    aahed
    Last edited by wei; 01-03-2018 at 07:36 AM.

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Why do you try to read from a file you just created for writing on line 17 ?
    On line 54 just write a \n after you have written the word.
    Also when you allocate memory you should deallocate it to avoid memory leaks.
    Why do you want to stor them when do don't use them?
    Why not just split the line after you read it and write it to the output?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple condition for single variable in single line
    By merafiq in forum C Programming
    Replies: 2
    Last Post: 03-13-2016, 05:26 PM
  2. Is there any way to have more than one input on a single line?
    By PYROMANIAC702 in forum C Programming
    Replies: 8
    Last Post: 07-01-2011, 08:33 AM
  3. Single line user inputted string to Array?
    By Sparrowhawk in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 08:10 PM
  4. removing a single string/line from a text file
    By turmoil in forum C Programming
    Replies: 5
    Last Post: 04-06-2005, 03:44 PM
  5. Single-line edit box?
    By BubbleMan in forum Windows Programming
    Replies: 1
    Last Post: 09-09-2001, 08:59 PM

Tags for this Thread