Solved

Reading Files into arrays in C

Posted on 2002-03-18
7
219 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
Technology Partners: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 73
C++ Language error 28 316
max float value 3 75
HTTPSendRequest with WinINet delays on first call 11 120
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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