Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading Files into arrays in C

Posted on 2002-03-18
7
Medium Priority
?
230 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

773 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