Results 1 to 2 of 2
  1. #1
    bakchodpappu99's Avatar
    Join Date
    Jul 2016
    Gender
    male
    Posts
    882
    Reputation
    17
    Thanks
    63
    My Mood
    Angry

    Coding Help C, Text file into 2d array.

    I am stuck in the C programming and would like if someone could help. I need to take the data from a .txt file and store it in a 2d array. I can read the data and store it like this.

    If you could show me an example of how I can store this in 2d array it'll be greatly appreciated. Thanks

    FILE* inFile = NULL;
    char Arr[1000];
    inFile = fopen("myfile.txt", "r");

    while(1){
    fgets(Arr, 1000, inFile);
    printf("%s", Arr);
    if(feof(inFile))
    break;
    }

  2. #2
    ChristopherFisher's Avatar
    Join Date
    Dec 2019
    Gender
    male
    Location
    Florida
    Posts
    48
    Reputation
    10
    Thanks
    1
    My Mood
    Daring
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    // Allocate memory for the array like this:

    int** array;

    array = malloc(n * sizeof(*array)); /* Assuming `n` is the number of rows */
    if(!array) /* If `malloc` failed */
    {
    fputs(stderr, ""Error allocating memory; Bailing out!"");
    exit(-1);
    }

    int count = 1;
    for(int i = 0; i < n; i++)
    {
    array[i] = malloc(count * sizeof(**array));
    if(!array[i]) /* If `malloc` failed */
    {
    for(int j = 0; j < i; j++) /* free previously allocated memory */
    {
    free(array[j]);
    }
    free(array);

    fputs(stderr, ""Error allocating memory; Bailing out!"");
    exit(-1);
    }
    count++;
    }

    // Then, read data from the file into the array by using:

    FILE* fp = fopen(""file.txt"", ""r"");
    if(!fp)
    {
    for(int i = 0; i < n; i++) /* free previously allocated memory */
    {
    free(array[i]);
    }
    free(array);

    fputs(stderr, ""Error opening 'file.txt'; Bailing out!"");
    exit(-1);
    }

    int max = 1;

    for(int i = 0; i < n; i++)
    {
    for(count = 0; count < max; count++)
    {
    fscanf(fp, ""%d"", &array[i][count]);
    }
    max++;
    }

    // Then print the results:

    max = 1;

    for(int i = 0; i < n; i++)
    {
    for(count = 0; count < max; count++)
    {
    printf(""array[%d][%d] = %d"", i, count, array[i][count]);
    }
    max++;
    }

    // And finally, free the allocated memory:

    for(int i = 0; i < n; i++)
    {
    free(array[i]);
    }
    free(array);
    }
    Providing PayPal solutions, any help regarding VCC, VBA, Stealth account, limitation should be get from here: PayPal Unlocked

Similar Threads

  1. Replies: 4
    Last Post: 04-16-2013, 02:32 PM
  2. Need help inserting mod files into the game.
    By Username4 in forum Vindictus Help
    Replies: 9
    Last Post: 02-18-2013, 02:51 PM
  3. [Help Request] Need help in converting DAT map files into LTA.
    By omeron122 in forum Combat Arms Mod Help
    Replies: 0
    Last Post: 11-12-2012, 11:58 AM
  4. Coding Help For Rez File
    By FORCE™ in forum Combat Arms Mod Discussion
    Replies: 7
    Last Post: 12-02-2009, 07:53 AM
  5. help me put hotkeys into my coding pelase
    By DylanOwnsYou in forum Visual Basic Programming
    Replies: 11
    Last Post: 09-15-2009, 12:01 PM

Tags for this Thread