Solved

cant get the command line right

Posted on 2001-06-23
11
238 Views
Last Modified: 2011-08-18
i am trying to get some data from the file,but for some reason i dont seame to get the line correct.
here is what i have done:
          if(temp==k)
          {
               puts("{");
               for(j=0;j<size;j++)
               {
                    fscanf(fd+j,"%d",temp1);
                    printf(" %d ,",temp1);
               }
          }
i tried puting & before temp1,but it doesnt help-it still doesnt get the int from the file.
what am i doing wrong?
0
Comment
Question by:yabelson
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 6

Expert Comment

by:zebada
ID: 6221859
Remove the j from the file descriptor argument in fscanf.
and add the & to temp1...

         if(temp==k)
         {
              puts("{");
              for(j=0;j<size;j++)
              {
                   fscanf(fd,"%d",&temp1);
                   printf(" %d ,",temp1);
              }
         }
0
 
LVL 11

Expert Comment

by:griessh
ID: 6221869
yabelson

Before you post new question, could you please close your old question about the same issue in this area.

Thanks

======
Werner
0
 
LVL 1

Author Comment

by:yabelson
ID: 6221888
qriessh-done.
zebada-i tried that,it didnt work...
0
Industry Leaders: 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 6

Expert Comment

by:zebada
ID: 6223112
What is it you're trying to read?
Post a sample of the data file.
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6224462
and please tell us how temp 1 is declared.

   int i;

   if (fscanf(fd, "%d", &i))
      printf("The integer read was: %i\n", i);
   else
   {
      fprintf(stderr, "Error reading an integer from '<file name>' .\n");
      exit(1);
   }


0
 
LVL 1

Author Comment

by:yabelson
ID: 6225690
temp1 is declared an integer.
if you want to check what this is about,look ay my other q's.
0
 
LVL 1

Expert Comment

by:gstoddart
ID: 6226165
Your big problem is that you think a file is an array.  It isn't.  When you read from a file, you read each byte sequetially until you run out of file.  Unless you do a seek of course.

The code "fd + j"  says "add the value j to the value stored in fd".  This will *not* hit your file descriptor.  If you look at the return code of the fscanf() you will determine that the function returned an error.

I'm assuming 'size' is a predetermined integer which indicates the number of items you are trying to read and was goten elsewhere.

Make sure fd has been declared "FILE *fd;" to ensure you have the right kind of file open (a stream, which is what all of the fopen, fclose etc functions operate on.

Unless you have written a function called puts(), it is not a valid C function and is in error.

0
 
LVL 1

Author Comment

by:yabelson
ID: 6229261
fd is declared FILE *fd.
how *can* i go through the different parts of the file?
i understood that i cant do fd+j,but i stilldidnt understand how to move within the file itself.
0
 
LVL 1

Accepted Solution

by:
gstoddart earned 25 total points
ID: 6229413
The act of reading the fle advances you to th next spot in the file.  A file is a sequential-access medium (it can be used otherwise, but I suspect you don't need that.)

If you read x number of bytes from a file, the pointer into the file advances by that x bytes until you hit EOF.  Think of it as pulling a thread, you just pull the thread and it follows you.

Essentially, you'll be doing:

int result;

do {
     result = fscanf(fd, "%d", &temp1);

     if (result != EOF) {
          /*  Got an int, do something  */
     }
} while (result != EOF);


Clearer?

0
 
LVL 1

Author Comment

by:yabelson
ID: 6235564
yes,thanks,now i understand what i sholud have done,and the whole ex.is clearer...
now i hope i am ready for the exam...
0
 
LVL 1

Author Comment

by:yabelson
ID: 6235566
i may be thick headed with this FILE issue,but now i ubderstand a  lot better...
0

Featured Post

Industry Leaders: 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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

685 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