Solved

Reading Files into arrays in C

Posted on 2002-03-18
7
222 Views
Last Modified: 2010-04-02
How would i read info from a atext file into an array in C? I know how to do it in C++ as this is a lot easier:

while(!infile.eof)
{
 infile>>arr1[loop];
 infile>>arr2[loop];
 loop++;
}

But how do i do it in C using FILE?
I have tried the following but it just comes up with the same values in each position in the array.

while (fscanf(FilePtr,"%s %s",&S,&D) != -1)
{
 Type[loop] = D;
 Quant[loop] = S;
 loop++;
}

Thankyou...


Rickardo
0
Comment
Question by:rickardo
[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
7 Comments
 
LVL 11

Accepted Solution

by:
griessh earned 50 total points
ID: 6876923
You are a C question in the C++ area here :-)

strcpy (&Type[loop], &D);

======
Werner
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 6876926
It would help if you showed the declarations of the variable you're using, but I think the problem is that you can't copy strings in C by simply assigning them--all that will do is copy the value of the base pointer of the string, which as you rightly say is the same all the way through. You need to use something like:

strcpy(Type[loop], &D);
strcpy(Quant[loop], &S);

(Note I assume you're using strings because of the %s specifiers in fscanf--if these values AREN'T strings then you're going wrong there!).

One other thing: I hope you put a bit more error checking when you actually use these loops in a program, because at the moment there's nothing to stop the code writing beyond the end of the array and causing major memory corruption.
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 6876929
griessh: Curse my slow typing... :-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:griessh
ID: 6876958
Not slow ... too much <grin>

======
Werner
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6878779
It might work to add \n to the scanf format string.  

while (fscanf(FilePtr,"%s %s\n",&S,&D) != -1)

I have never used scanf, because it is so dangerous (so many ways something can go wrong).  Better to read the line as a string and parse out the data.

-- Dan
0
 
LVL 11

Expert Comment

by:griessh
ID: 6955428
Dear rickardo

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "griessh"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6978502
Per recommendation, force-accepted by
Netminder
CS Moderator
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

615 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