Thread: Help with pasting strings to a .txt file

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    4

    Help with pasting strings to a .txt file

    Hello,

    I have some trouble with my homework and I can't find the problem.

    my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "input3.h"
    
    
    int main(int argc, char **argv) {
       if (argc < 3) {
            printf("Aufruf: %s <anzahl> <bundesland>\n", argv[0]);
            printf("Beispiel: %s 100 Bayern\n", argv[0]);
            printf("Klein-/Großschreibung beachten!\n");
            exit(1);
        }
        int anzahl = atoi(argv[1]);
        char *bundesland = argv[2];   
    
    
        
        char staedte[MAX_LAENGE_ARR][MAX_LAENGE_STR];
        char laender[MAX_LAENGE_ARR][MAX_LAENGE_STR];
        int bewohner[MAX_LAENGE_ARR];  
    
    
        int len = read_file("staedte.csv", staedte, laender, bewohner);
    
    
    char *result[len];
    int j, i=0;
    int diff = 1;
    int min =1;
    
    
    
    
    
    
    for (j=0; j<len; j++)  {
        result[j] = (char*) malloc(100*sizeof(char));
    diff = strcmp(bundesland, laender[j]);
    min = bewohner[j] - anzahl;
    
    
    
    
    
    
     if (diff ==0 && 0 <= min)  {
     snprintf(result[i], 100, "Die Stadt %s hat %d Einwohner.", staedte[j], bewohner[j]);
        i++;
                    }  
    }
                                 
    write_file(result, len);
     free(*result);
    }
    you open the code with ./<program name> <number> <country> and it pastes on a .txt file every city of the country + its population but only those whose population is higher than the entered number.

    My problem is that there are multiple empty lines and I don't know what to do.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Indenting your code would be a start.

    > write_file(result, len);
    But the number of valid records found is given by 'i', not 'len'.

    > free(*result);
    This only frees the first result you malloc'ed, not all of them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    4
    Thank you so much, found the problem now .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pasting code form a .txt file...
    By Rune Hunter in forum C++ Programming
    Replies: 100
    Last Post: 09-10-2004, 04:54 PM
  2. copying and pasting
    By cathal in forum C Programming
    Replies: 4
    Last Post: 09-07-2004, 09:13 AM
  3. Token pasting and a little more
    By Mario in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2002, 05:02 PM
  4. File Copying+Pasting...
    By Ion Blade in forum C++ Programming
    Replies: 2
    Last Post: 05-20-2002, 12:47 AM
  5. Token pasting
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2002, 09:39 PM

Tags for this Thread