Solved

Linux C Question

Posted on 2007-11-20
2
207 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
2 Comments
 
LVL 84

Expert Comment

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

Accepted Solution

by:
Jeff Darling earned 125 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

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

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…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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

13 Experts available now in Live!

Get 1:1 Help Now