Sign in with
Sign up | Sign in
Your question

C Programming Question

Last response: in Applications
Share
December 8, 2008 5:54:15 AM

Thought I would post this here as it is the forum closest to this topic that I could find.

I'm trying to write this program for class, and I'm having trouble doing part of it.

What it needs to do is read a file and put the data into arrays. The file has 4 lines, with double variables on each line, separated by spaces (2 spaces for some reason). Each line needs to be entered into a separate array, so that there are 7 numbers in the first array, 7 in the second, 3 in the 3rd, and 3 in the fourth. He says we are supposed to use fscanf in order to read in the data, but I thought I read that it did not work with spaces. We are using loops to enter each variable, so there will be 4 loops total, 1 for each array/line.

The text file looks like:

xxxx xxxx xxxx xxx xxx xxxx xxxx
xxxx xxx xxxx xxx xxx xxxx xxxx
xxx xx xxx
xxxx xx xxxx

With the x's being numbers.

It has to be using C, no C++ commands.

THanks for any help in advance.

More about : programming question

a b L Programming
December 8, 2008 7:57:25 AM

Do you know, in advance, the number of doubles per line? If so you could use a sequence of reads like:

fscanf(%d %d %d %d %d %d %d, &var1, &var2, &var3, &var4, &var5, &var6, &var7);
fscanf(%d %d %d %d %d %d %d, &var1, &var2, &var3, &var4, &var5, &var6, &var7);
fscanf(%d %d %d, &var1, &var2, &var3);
fscanf(%d %d %d, &var1, &var2, &var3);

with appropriate code between the reads to put the variables into your arrays.

If you don't know the format of the file in advance things are a little more tricky if you're restricted to fscanf().
a b L Programming
December 8, 2008 9:01:22 AM

I would edit my previous post, but this %^&*$% forum software won't let me.

I'm sure you noticed the elementary mistakes that I made in my hasty post! The fscanf calls should read:

fscanf(f, "%d ...", &var1, ...)
Related resources
December 8, 2008 4:09:42 PM

Thanks for the reply, but I need to try to read in that data using for loops. I know how times each one has to iterate though, so it should be easier.
I have tried this so far:

double Tpres[NP], Psat[NP], Tsat[NH], HGsat[NH];

FILE *data;
data = fopen("me30-proj10-data.txt","r");
if(data == NULL)
{ printf("There was an error opening the file.\n");

}
for(int count1 = 0; count1 <= NP; count1++)
{
fscanf(data, "%lf", Tpres[count1]);
}
for(int count2 = 0; count2 <= NP; count2++)
{
fscanf(data, "%lf", Psat[count2]);
}
for(int count3 = 0; count3 <= NH; count3++)
{
fscanf(data, "%lf", Tsat[count3]);
}
for(int count4 = 0; count4 <= NH; count4++)
{
fscanf(data, "%lf", HGsat[count4]);
}


But, every time I run it, I get an error: "An Access Violation (Segmentation Fault) raised in your program", during the first for loop.
Any ideas?
December 8, 2008 4:12:17 PM

Ok, ignore my last post, i think i fixed it. (Missing & on variables).
:ange: 
a b L Programming
December 9, 2008 2:12:58 PM

That's easy (I think!). You're counting from 0 to NP (i.e. NP + 1 times) but your array only has NP elements. Classic C mistake. Try changing the "<=" to "<" in the for loops.

Just seen your second post - that too!
!