Solved

Reading Files into arrays in C

Posted on 2002-03-18
7
202 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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 55
why "." vs "->" 23 120
How to print into std::string like sprintf using C++11? 12 59
object oriented programming comparison 5 69
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

785 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