?
Solved

Linux C Question

Posted on 2007-11-20
2
Medium Priority
?
214 Views
Last Modified: 2010-04-22
#include <stdio.h>
int main()
{
      FILE *fp;
       fp = fopen("test.txt", "w");
        int a[100];
        int count=0;
        int i;
        for(i=0; i < 100; i++)
              a[i]=i;
                                                                                                                             
        for(i=0; i < 100; i++)
       {
                if(count > 10)
                {
      fp=fopen("test.txt", "w");
      //fseek(fp, 0, SEEK_SET);
                     //rewind(fp);
                        count=0;
                }
                fprintf(fp, "%d  ", a[i]);
                count++;
         }
}

Please refer the code snippet above, I expect 90 to 100 to be in the file. But there is 1 to 10 in the file.
Could you please suggest a solution
0
Comment
Question by:sudarshantk
[X]
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
2 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 20324457
fclose(fp);
0
 
LVL 13

Accepted Solution

by:
Jeff Darling earned 375 total points
ID: 20324540
You will definitely need fclose, but that won't solve all your problems.

You should close the file before attempting to open it again.

The way your code is setup, you will open and close the file 10 times.

your file will only have 99 in it at the end if you add fclose(fp) before the open in the If statement and after exiting the 2nd for loop.


#include <stdio.h>
int main()
{
      FILE *fp;
        int a[100];
        int count=0;
        int i;
 
        // open the file
        fp=fopen("test.txt", "w"); 
 
        // fill the array
        for(i=0; i < 100; i++) a[i]=i;
                                                    
        for(i=90; i < 100; i++)
        {
         fprintf(fp,"%d  ",a[i]);                 
        }
        fclose(fp);
        return 0;
}

Open in new window

0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

650 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