Solved

Reading Files into arrays in C

Posted on 2002-03-18
7
182 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
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now