Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-09
11
Medium Priority
?
399 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:reesee324
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 35089331
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
 

Author Comment

by:reesee324
ID: 35094877
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
 
LVL 5

Assisted Solution

by:Kendor
Kendor earned 500 total points
ID: 35095058
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:Cha1tu
ID: 35095114
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
 

Author Comment

by:reesee324
ID: 35095264
Ok thanks guys!
0
 
LVL 2

Expert Comment

by:Cha1tu
ID: 35095490
Please accept a solution and award points if you are satisfied reesee
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 35095507
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35095555
>> 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
 
LVL 5

Expert Comment

by:Kendor
ID: 35095857
Actually you should give points to the post that answered your question. Not as you did.
0
 

Author Comment

by:reesee324
ID: 35098416
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question