Solved

Linux C Question

Posted on 2007-11-20
2
211 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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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