• C

How do I read information from a text file and store it into an array in C?

I don't understand how to load the file and then read the info and store it in an array. This is what the file looks like:
125
LIPSO FACTO
SC
A
118.030000
61
193
TAMMALIZATION CO.
SC
B
3728.000000
23
237
MORE MATZOES
TN
A
846.290000
12

And each line should consist of a cust id, customer name, state, discount code, balance due, and outstanding orders. How would I go about storing each of the lines into each of the seperate arrays?
reesee324Asked:
Who is Participating?
 
phoffricConnect With a Mentor Commented:
Define a struct that contains the 6 items. For the strings to keep things simple, you can use char array with a size much larger than the max size anticipated by you.

Read one line at a time using fgets. Here is the help on fgets.
     http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

Read it carefully and look at its code example shown here (the idea is to steal from the examples and mold it into your own needs):
/* fgets example */
#include <stdio.h>

int main()
{
   FILE * pFile;
   char mystring [100];

   pFile = fopen ("myfile.txt" , "r");
   if (pFile == NULL) perror ("Error opening file");
   else {
     fgets (mystring , 100 , pFile);
     puts (mystring);
     fclose (pFile);
   }
   return 0;
}

Open in new window

To get familiar with this code (and other sample code below), run it in a debugger and see what happens as you step over the fgets.

To get an array of structs, you could say:
    struct myRecords[100];
Now you have defined 100 structs.

You have an integer type in your struct. To convert the ASCII string to integer, you can use atoi:
     http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
or even atol:
     http://www.cplusplus.com/reference/clibrary/cstdlib/atol/
/* atoi example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char szInput [256];
  printf ("Enter a number: ");
  fgets ( szInput, 256, stdin );
  i = atoi (szInput);
  printf ("The value entered is %d. The double is %d.\n",i,i*2);
  return 0;
}

Open in new window

To convert an ASCII string to float (or double), you can use atof:
     http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
/* atof example: sine calculator */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main ()
{
  double n,m;
  double pi=3.1415926535;
  char szInput [256];
  printf ( "Enter degrees: " );
  gets ( szInput );
  n = atof ( szInput );
  m = sin (n*pi/180);
  printf ( "The sine of %f degrees is %f\n" , n, m );
  return 0;
}

Open in new window


If you haven't used a debugger, then I recommend downloading the free Microsoft Visual Studio Expresss C++ 2010:
    http://www.microsoft.com/express/Downloads/

To quickly get started (about 15 minutes learning curve), you can read:

   C/C++ Beginner's Debugging Guide

After becoming familiar with the basics, move onto these two articles:
   Breakpoint Tips for C/C++

   Watch, Memory, Stack Tips: C/C++
0
 
phoffricCommented:
Your question is really a request for us to do this assignment for you, and this is against EE academic policy. You may get a response if you write code that has a problem (e.g., doesn't compile) and we'll help you compile it. Then you can ask a different question if you don't get the desired results, and we can try to point out a problem in your program.
0
 
reesee324Author Commented:
Well the problem is I don't even understand how to start writing the code to even begin to make it work. I understand how to open the file and then you would use a while (!feof(fp)). This is my first year using c but I didn't know if you could do cust_id = get_int(fp) and so on and so forth for each type. If I had any idea of how to write it then I would.
0
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

 
KendorConnect With a Mentor Commented:
what if you try asking google for the same. go through some tutorials on this topic.

for example those:
http://www.cprogramming.com/tutorial/cfileio.html
http://www.dreamincode.net/forums/topic/172179-reading-multiple-lined-file-into-array-c/
http://answers.yahoo.com/question/index?qid=20080902075641AAq7zwM

if you tried and have written some code yourself - you'll get help on specific questions here for sure
0
 
Cha1tuCommented:
Hi reesee,

Well, there are several ways to do this. Try to do some research on getline(), getchar() function, fstream, ifstream variables.

I can say start from small code snippets make them work. You can progress step by step. First write a program to open a file and read the characters or lines from it. Then write a program to insert characters or strings into array. Now think of using both programs together.

Thank you,
Chaitu
0
 
reesee324Author Commented:
Ok thanks guys!
0
 
Cha1tuCommented:
Please accept a solution and award points if you are satisfied reesee
0
 
phoffricCommented:
>> Try to do some research on getline(), getchar() function, fstream, ifstream variables.
    These are good suggestions if you wish to write C++ code. If so, you should in the future select the C++ zone.

From all the above suggestions, you should try putting the pieces together by building a little at a time, getting just a little to work (like read a line in, for example). Then add on another little piece of the puzzle.
0
 
KendorCommented:
Actually you should give points to the post that answered your question. Not as you did.
0
 
reesee324Author Commented:
Ok thanks! I appreciate the help and I'm still trying to get used to this points system. I just registered with ya'll so I'm still trying to get the hang of things!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.