Jessi Brattain
Jessi Brattain

Reputation: 13

Make array read file and store all data

Okay, so all my code is functional. I am mostly looking for suggestions.

Right now, I have a file being read. Each line of the file has 3 different variables. These variables are being read into an array. The problem I am trying get input on it that as the file is read in the while loop, the data overwrites itself. I need all the data stored in one array with spaces between. I a not sure what it is not currently doing that. Is there a better function to be using?

Here is a sample of what I have:

            char filepath[1000], filepathBP1[1000]; 
            char BP2_ext [] = "\\BP_2.txt";
            char bp2_Val1[80], bp2_Val2[80], bp2_Val3[80], bp2_Line[100];
            FILE* fp;

            strcpy(filepathBP1, filepath);
            strcat(filepathBP1, BP1_ext);
            fp = fopen(filepathBP1, "r");

            if (fp == NULL)
            {
                puts("ERROR OPENING FILES");
                exit(EXIT_FAILURE);
            }
            while (!feof(fp))
            {
                printf("\n\nREADING BP_1.txt...");
                fgets(bp1_Line, 100, fp);
                sscanf(bp1_Line, "%s\t%s\t%s", bp1_Val1, bp1_Val2, bp1_Val3);
                printf("%s\t%s\t%s\n", bp1_Val1, bp1_Val2, bp1_Val3);
            }
            fclose(fp);

Upvotes: 0

Views: 82

Answers (2)

user4925
user4925

Reputation: 209

Here is a modified version of your code. Note this is just a basic solution. Please feel free to modify the code according to your needs. Now your basic idea/approach is correct. The only thing you need to do is that you have to have an array of "Strings" to store the "strings". Also, your question isn't clear. Please be more specific as to what you finally want the output to result in or look like.

Now in my program I have 3 array of "strings" variables. And each of these store the strings of a column of strings.

For example if the file data is like this,

abc def zxc
qwe rty uio

Then line_list1 will store strings abc,qwe, line_list2 will store the strings def,rty and line_list3 will store the strings zxc,uio. Now I don't know if this exactly what you want(since you haven't been specific what the resulting output should be/do/look like.), but, this program will give you an idea to make your program work.

Here is the program,

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100

int main(){

    char bp1_Line[MAX];
    char *line_list1[MAX],*line_list2[MAX],*line_list3[MAX];
    int index=0,i=0;
    FILE* fp=NULL;

    fp = fopen("data.txt", "r");

    if (fp == NULL){
       puts("ERROR OPENING FILES");
       exit(EXIT_FAILURE);
    }
    while ( fgets(bp1_Line, MAX, fp)!= NULL && index<MAX){
        printf("READING:%s\n",bp1_Line);
        if(sscanf(bp1_Line, "%s\t%s\t%s", &line_list1[index], &line_list2[index], &line_list3[index]) == 3){
           strcpy(bp1_Line,"");
           index++;
        }
    }
    fclose(fp);

    for(i=0;i<index;i++){
       printf("%s\t%s\t%s\n", &line_list1[i], &line_list2[i], &line_list3[i]);
    }

    return 0;
}

Or if you wanna store all those strings/words in one array of strings then change the above code while loop section to this code,

 while ( fgets(bp1_Line, MAX, fp)!= NULL && index<MAX){
   printf("READING:%s\n",bp1_Line);
   if(sscanf(bp1_Line,"%s\t%s\t%s",&list[index],&list[index+1],&list[index+2]) == 3){
      strcpy(bp1_Line,"");
      index=index+3;
   }
 }

Upvotes: 1

Roland Illig
Roland Illig

Reputation: 41625

You should switch to another programming language. Python may be good for you.

  • You left out most of the error handling. Python will throw exceptions in such a case, which are hard to ignore.
  • You use fixed-length character arrays without checking for overflow. Python has built-in string support.
  • Python has built-in support for data structures like resizable sequences and even dictionaries.

Upvotes: 0

Related Questions