Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

cant get the command line right

Posted on 2001-06-23
11
232 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to declare a 2-dimensional version of strlen 4 218
XCode crashes while uploading to app store. 5 265
Acrinis True image 2 92
Resolve Dependency Issues 4 79
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

840 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