0

I have been stuck on this for a while. I am not sure how to print our all the lines from my file in to C terminal and then save it into a data structure. My code is as following:

#include <stdio.h>
#include <stdlib.h>
#include "structure.h"

int main()
{
    printf("Hello world!\n");
    int i = 43;// number of iteration to save data in an array
    int x;
    FILE *fptr;
    struct values *valuesPtr, values[x];
    valuesPtr = &values[x];


        if((fptr = fopen("energy.txt", "r")) == NULL)
        {
            printf ("Error opening file ");
            return 0;
        }
        while (fptr != NULL)
        {
        for(x = 0; x < i; x++ )
        {
            fscanf(fptr, "%s %s %d", &valuesPtr->start_vertex, &valuesPtr->destination_vertex, &valuesPtr->num);
            printf("\nStart vertex: %s \nDestination vertex: %s \nWeight: %d\n\n", valuesPtr->start_vertex, valuesPtr->destination_vertex, valuesPtr->num);
        }
        }
       fclose(fptr);
    return 0;
}

My structure.h file has the following structure:

struct values{
        int num;
        char start_vertex[250];
        char destination_vertex[250];
        };

It currently only shows the first line from my file. I want it to read all the lines from the file and then save that data in a data structure. Could you also tell me what the best data structure I could use to save all of the elements from my file in memory to be used later on.

1 Answers1

0

The way you initialize valuesPtr is not exact. valuesPtr = &values[x]; is failed because the maximum index that array values can take is x-1 (from 0 to x-1). It is a pointer, so allocate it (do not forget initialize x first):

 struct values *valuesPtr = malloc(sizeof(struct values) * x);
 if (!valuesPtr)
    return -1;

Or using 1D array:

/*init x here*/
struct values valuesPtr[x]

And if you want to get value by using fscanf, do not use & for char array. Use as below:

int num =  fscanf(fptr, "%s %s %d", valuesPtr[i].start_vertex, valuesPtr[i].destination_vertex, &valuesPtr[i].num);
// verify the the number (num) of input items successfully matched and assigned if you want.

UPDATE test:

 struct values{
    int num;
    char start_vertex[250];
    char destination_vertex[250];
  };

int main()
{
    printf("Hello world!\n");
    int x = 3; // The number of lines that you want to get from file.
    FILE *fptr;
    struct values *valuesPtr = malloc(sizeof(struct values) * x);

    if (!valuesPtr)
        return -1;


    if((fptr = fopen("text.txt", "r")) == NULL)
    {
        printf ("Error opening file ");
        return 0;
    }

    for(int i = 0; i < x; i++ )
     {
            fscanf(fptr, "%s %s %d", valuesPtr[i].start_vertex, valuesPtr[i].destination_vertex, &valuesPtr[i].num);
            printf("\nStart vertex: %s \nDestination vertex: %s \nWeight: %d\n\n", valuesPtr[i].start_vertex, valuesPtr[i].destination_vertex, valuesPtr[i].num);
     }
     free(valuesPtr);
     fclose(fptr);
    return 0;
}

text.txt

ab cd 9
ef gh 10
ii cc 11

The output:

Hello world!

Start vertex: ab 
Destination vertex: cd 
Weight: 9


Start vertex: ef 
Destination vertex: gh 
Weight: 10


Start vertex: ii 
Destination vertex: cc 
Weight: 11
Hitokiri
  • 3,607
  • 1
  • 9
  • 29
  • I am not sure what you are doing in the first section of the code. Could someone please explain that to me – Usama Navid Apr 12 '20 at 13:30
  • you are not sure for what ? – Hitokiri Apr 12 '20 at 13:33
  • Where in the code should I add the: "struct values *valuesPtr = malloc(sizeof(struct values) * x); if (!valuesPtr) return -1; " And the reason behind adding that code – Usama Navid Apr 12 '20 at 13:35
  • `valuesPtr` is a pointer that points to struct values. So if you want to use the pointer, you have to allocate (using malloc for example) it. You test the malloc is successful or not, if not you exit your program – Hitokiri Apr 12 '20 at 13:38
  • Perfect, thanks. How should I modify my code in order to get the desired results? – Usama Navid Apr 12 '20 at 13:41
  • Thanks a lot, mate, it works now. To save all of those lines and the start, destination and weight values, do I have to make a linked list? If so how do I go about doing it? – Usama Navid Apr 12 '20 at 14:08
  • you can read line by line (using `getline()` function) until get the end of file (EOF). For this case, using `sscanf` instead of `fscanf` to read the value from the string (the result of getline() function). You do not need to use the linked-list – Hitokiri Apr 12 '20 at 14:12
  • Mate is it possible, if you could show it to me as an example like you did before. I am so sorry for wasting a lot of your time – Usama Navid Apr 12 '20 at 14:15
  • This link will help you. https://stackoverflow.com/questions/9206091/going-through-a-text-file-line-by-line-in-c. Use `sscanf` for `line` in your case. You have to code from the scratch. it makes you become better and learn from the errors and warning in code. – Hitokiri Apr 12 '20 at 14:24
  • Thanks a lot I will give it a go now. Is it possible if I can talk to you on gmail hangout or Facebook? – Usama Navid Apr 12 '20 at 14:28
  • sorry i dont want to public my info. If you have the question, post here, i'm glad to help you if you want. In SO there are alot of expert. I'm just beginner. – Hitokiri Apr 12 '20 at 14:33
  • No problem mate :) – Usama Navid Apr 12 '20 at 14:37