Solved

Linux C Question

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

864 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

22 Experts available now in Live!

Get 1:1 Help Now