Thread: Saving data into a array with snprintf

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    31

    Saving data into a array with snprintf

    Hello,

    I have this code which read some data from a text file. Then I save the data into a 2D array dynamically with malloc. Until codeline 57 this works fine. Then I would like to append some lines.
    But at code line 59 I would like save the manipulated data into the array: char appendedLine[MAX_LINE_LENGTH], and print it.
    But this does not work, and I did not know why. Can anybody help me to solve this problem?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_LINE_LENGTH 300    // maximal length of a line of the input file
    #define MAX_LINES 100000       // lines of input file
    
    int main()
    {
    
    
        // Read the lines from the file
            FILE *fp = fopen("input2.txt", "r");
        if (fp == NULL)
        {
            printf("Failed to open the file.\n");
            return 1;
        }
    
        // Allocate memory for a 2D array dynamically
        char **lines = (char **)malloc(MAX_LINES * sizeof(char *));
        if (lines == NULL)
        {
            printf("Failed to allocate memory for lines.\n");
            return 1;
        }
    
        for (int i = 0; i < MAX_LINES; i++)
        {
            lines[i] = (char *)malloc(MAX_LINE_LENGTH * sizeof(char));
            if (lines[i] == NULL)
            {
                printf("Failed to allocate memory for line %d.\n", i);
                return 1;
            }
        }
    
    
        int lineCount = 0;
        char line[MAX_LINE_LENGTH];
    
        while (fgets(line, sizeof(line), fp) != NULL)
        {
            if (line[strlen(line) - 1] == '\n')     // at the end of each line \n will be deleted
                line[strlen(line) - 1] = '\0';
            strcpy(lines[lineCount], line);
            lineCount++;
            if (lineCount >= MAX_LINES)
            {
                printf("Exceeded maximum number of lines.\n");
                break;
            }
        }
    
        fclose(fp);
    
        // Append line
        char appendedLine[MAX_LINE_LENGTH];
        snprintf(appendedLine[0], MAX_LINE_LENGTH, "%s%s%s\n", lines[3], lines[22], lines[41]);
        snprintf(appendedLine[1], MAX_LINE_LENGTH, "%s%s%s\n", lines[5], lines[24], lines[43]);
        snprintf(appendedLine[2], MAX_LINE_LENGTH, "%s%s%s\n", lines[7], lines[26], lines[45]);
    
        printf("%s\n", appendedLine[0]);
        printf("%s\n", appendedLine[1]);
        printf("%s\n", appendedLine[2]);
        
    
        // Free the dynamically allocated memory
        for(int i = 0; i < MAX_LINES; i++)
        {
            free(lines[i]);
        }
        free(lines);
    
        return 0;
    }
    Last edited by jasmin89; 06-03-2023 at 01:56 PM.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Have you compiled with warnings turned on?

    I guess not, because appenededline[0] is a character, not an array you can snprintf() into safely.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try
    Code:
        char appendedLine[3][MAX_LINE_LENGTH*3+1];
        snprintf(appendedLine[0], sizeof(appendedLine[0]), "%s%s%s\n", lines[3], lines[22], lines[41]);
        snprintf(appendedLine[1], sizeof(appendedLine[1]), "%s%s%s\n", lines[5], lines[24], lines[43]);
        snprintf(appendedLine[2], sizeof(appendedLine[2]), "%s%s%s\n", lines[7], lines[26], lines[45]);
    1. You need 3 of them
    2. Each needs to be 3* the length of the combined strings, +1 for the newline.

    > printf("%s\n", appendedLine[0]);
    Or maybe you have the newline here instead.

    But now you have two newlines.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2020
    Posts
    31
    Thank You for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2022, 10:38 AM
  2. Need help around saving data.
    By flumesoft in forum C# Programming
    Replies: 5
    Last Post: 04-30-2008, 06:27 PM
  3. Saving data
    By bradszy in forum C++ Programming
    Replies: 4
    Last Post: 02-21-2008, 06:56 PM
  4. Replies: 20
    Last Post: 06-07-2007, 06:38 PM
  5. Replies: 2
    Last Post: 06-16-2005, 10:03 AM

Tags for this Thread