Solved

cant get the command line right

Posted on 2001-06-23
11
208 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

746 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