0

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);
2
  • Welcome to Stack Overflow. Please read the About page soon. More urgently, please read about how to create an MCVE (minimal reproducible example). We really need to see some sample data (3 lines is probably enough), and your required output and your actual output. Commented Apr 29, 2016 at 22:00
  • If you have line 1 = abc def ghi and line 2 = pqr stu vwx, and you want bp1_Val1 to end up with abc pqr and bp1_Val2 to end up with def stu and bp1_Val3 to end up with ghi vwx, then you have to take steps to add the blanks and read the second line after the data added by the first line. But that information should be in the question — which you can edit. Commented Apr 29, 2016 at 22:02

2 Answers 2

1

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;
   }
 }
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

This is a part of a final project in my Biomedical Engineering class, and it has to be in C. Trust me, I would much rather be using python, it makes way more sense with a project like this. This is just a small part of a larger project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.