Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8288501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:17:43+00:00 2026-06-08T12:17:43+00:00

I’m reopening and reading a file, SORTED.txt, after closing it from its first time

  • 0

I’m reopening and reading a file, SORTED.txt, after closing it from its first time use – copying all the contents of another file, UNSORTED.txt.
After copying from UNSORTED.txt, I wanted to count the number of lines of what I’ve copied (as a seperate process, and not during the copy process). It seems that fegtc() does not point at the beginning of the file (SORTED.txt) the second time over, hence the value of lines remains as what it was initialized as, 0. Also, in general, can I get the repointing of fgetc() done without closing and reopening the file in consideration?

Grateful for any help.

Cheers!

  f = fopen("./TEXTFILES/UNSORTED.txt", "w");
  if (f == NULL){
      printf("ERROR opening file\n");
      return 100;
  }

  for (i=0; i<1000000; i++){
    fprintf(f, "%d\n", (23*rand()-rand()/13));
  }
  fclose(f);

  f = fopen("./TEXTFILES/UNSORTED.txt", "r");
  if (f == NULL){
    return 100;
  }
  s = fopen("./TEXTFILES/SORTED.txt", "w");
  if (s == NULL){
    return 101;
  }

  while(1){
    j = getc(f);
    if (j == EOF) break;
    fputc(j, s);
  }
  fclose(f);
  //Closed source file. Read number of lines in target file.
  fclose(s);
  s = fopen("./TEXTFILES/SORTED.txt", "w");
  j = 0;

  while(1){
    j = fgetc(s);
    if (j == EOF) break;
    if (j == '\n') lines++;
  }

  fclose(s);
  printf("\n%d\n", lines);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:17:44+00:00Added an answer on June 8, 2026 at 12:17 pm

    It sounds like you’ve got it figured out! But since I went through the effort of putting this example together, I thought I’d post it anyways.

    #include <stdio.h> 
    
    int main()
    {
        FILE * f;
        FILE * s;
        int i, j;
        int lines = 0;
    
        f = fopen("./TEXTFILES/UNSORTED.txt", "w+");
        if (f == NULL){
            printf("ERROR opening file\n");
            return 100;
        }
    
        for (i=0; i<1000000; i++){
            fprintf(f, "%d\n", (23*rand()-rand()/13));
        }
    
        s = fopen("./TEXTFILES/SORTED.txt", "w+");
        if (s == NULL){
            fclose(f); // cleanup and close UNSORTED.txt
            return 101;
        }
    
        // rewind UNSORTED.txt here for reading back
        rewind( f );
    
        while(1){
            j = getc(f);
            if (j == EOF) break;
            fputc(j, s);
        }
    
        // done with UNSORTED.txt. Close it.
        fclose(f);
    
        // rewind SORTED.txt here for reading back
        rewind( s );
        j = 0;
    
        while(1){
            j = fgetc(s);
            if (j == EOF) break;
            if (j == '\n') lines++;
        }
    
        fclose(s);
    
        printf("\n%d\n", lines);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have a text area in my form which accepts all possible characters from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.