[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Linux C Question

Posted on 2007-11-20
2
Medium Priority
?
215 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 85

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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

829 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