Hey broskis!

I've only taken one class in introductory programming and my internship wants me to organize some excel data (it's in txt now). We never went over files in my c programming class so I'm struggling with this code a bit.

For practicing purposes my text looks like this. (it actually goes on for 330777 more lines).

20010929 0 2
20010929 15 2
20010929 30 2
20010929 45 2
.....
..
.

I'm trying to put this into a 3 by 330777 array. Since I'm a beginner, a 3 by 4 array will suffice.
Soooo basically i want my cout<<data[0][0] to be 20010929 and my cout<<data[3][2] to be 2.

My program thus far is not working out so any help would be greatly appreciated.

#include <stdio.h>   /* required for file operations */
#include <dos.h>  /* for delay */
#include <iostream>
#include <cstdio>

main(void){

   FILE *fr;            /* declare the file pointer */

   int i;

   int data[4][3], x, y;

   fr = fopen ("c:\\blahblah.txt", "r"); /* reads the file */
   fscanf (fr, "%d", &i);

   /*
   while (!feof (fr))    
    {
      printf ("%d\n ", i);
      fscanf (fr, "%d", &i);
    } */

   char line[256];                                               /* array for each line */
   for(x=0; x<2; x++){
                     fgets(line, 256, fr);                        /* reads each line of the .txt*/
                     y = 0;                                       /* rows start at zero */
                     while(sscanf(line,"%d", &data[y++][x]));     /* i found this line online so i dont really understand why there is a while and why its not working */
   }

   fclose(fr);                                                    /* close the file prior to exiting the routine */
   system("pause");
}

Recommended Answers

All 5 Replies

My program thus far is not working out...

Why is it no one likes to explain what the code does wrong and expects us to figure it out for ourselves?

Apologies.

If I knew what was wrong, there wouldn't be a problem.

Judging by my code, I'm obviously newb at programming and am having trouble doing a hopefully basic task (putting the .txt file in an array).

If your asking what happens when i compile, then the program just crashes.

Apologies.

If I knew what was wrong, there wouldn't be a problem.

Obviously you don't know what the problem in the code is, but something happens to make you say it doesn't work. That's what you need to describe clearly

If your asking what happens when i compile, then the program just crashes.

It obviously doesn't crash when you compile. If it did, the compiler crashed -- I doubt that. Therefore I assume you mean it crashes when you run the compiled program. Describe how it crashes -- error messages, any output, etc.

while(sscanf(line,"%d", &data[y++][x])); /* i found this line online so i dont really understand why there is a while and why its not working */

This is your problem.
1) If you don't know why there is a WHILE there, why did you put it there?
2) If you don't know why it's there and it's not working, maybe that's the problem.
3) Why would you just use a random line you found online and expect it to do what you want?

What is that line supposed to be doing? How many times?

char line[256];                                               
   for(x=0; x<2; x++){
                     fgets(line, 256, fr);                        
                     y = 0;                                       
                     while(sscanf(line,"%d", &data[y++][x]) > 0);     
   }
    printf("%d", data[0][0]);

Thanks for the reply WaltP.

People learn things in different ways. I personally enjoy to look at examples and toy around with them to test my understanding of the subject.

So it turns out sscanf returns an eof or a negative number so i fixed the crash by adding a >0. Now when i print the data[0][0], it gives me 1732752063 as opposed to 20010927. Any insight as to my mistake would be very welcoming, WaltP.

BOOM! Figured it out! This is my entire code now.

#include <stdio.h>                                              /* required for file operations */
#include <dos.h>                                                /* for delay */
#include <iostream>
#include <cstdio>
#include <iostream.h>

main(void){

   FILE *fr;                                                    /* declare the file pointer */
   int i;
   int data[4][3], x, y;

   fr = fopen ("c:\\blahblah.txt", "r");                        /* reads the file */

   for(x=0; x<4; x++){                                          // loops through the rows
                     y = 0;                                     //starts at first column during each row loop        
                     while(fscanf(fr,"%d", &data[x][y++]) > 0); // loops through the columns until each row ends.
   }


   //Outputs the correct data
   for (x = 0; x < 4; x++){
       for (y = 0; y < 3; y++){
          cout<<data[x][y]<<"   ";
          }
       cout<<endl;
   }


   fclose(fr);                                                  /* close the file prior to exiting the routine */                                
   system("pause");
}

However, my questions have not ended!!!!!!!!! The file I am dealing with has over 350770 rows and I have this feeling that using data[350770][3] as my array is terribly inefficient. I have no idea what I'm talking about though so any advice on that issue would be great! Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.