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

  • Home
  • SEARCH
  • 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 8214315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:25:13+00:00 2026-06-07T11:25:13+00:00

This code opens a directory, and for every file in the directory it loops

  • 0

This code opens a directory, and for every file in the directory it loops through every line of data inside the file, and then parses it to do some calculations and outputs the resulting data into a new file.

The problem is that I can only output a maximum of around 1021 files. I’m closing all of the fopens after outputting all the data, so I’m not sure what I’m doing wrong.

Shouldn’t fclose() be closing the open files therefore this not happening?

int main(int argc, char *argv[])
{

 //sample data values
  double lat;
  double lon;  
  double convergence;   
  double pt_scale;        
  int    zone = 54;              
  double major_axis = 6378137.0000;        
  double flattening = (1/298.2572);        
  double zoneWidth = 6;         
  double centMeridian = -177;      
  double falseEast = FALSE_EASTING;         
  double falseNorth = FALSE_NORTHING;
  double scale = SCALE_FACTOR;  

  int max_size = 128;
  int current_size = max_size;
  char *pathStr = malloc(max_size);
  char *outPathStr = malloc(max_size);
  char coData[100]; //max length of line;
  long firstTerm, secondTerm; //terms we will split the line into, lat, lon, elevation.
  int counter = 0; //pos counter
  int d = EOF; //end of file ASCII
  char strIn[200];
  char* elevation;
  char strOut[200];
  char dirOut[200]; //sprintf must use a actual defined buffer otherwise there will be a buffer overflow.
  char* cchr;
  int j;
  _setmaxstdio(2048);
  printf("Please enter the path of the files: \n");
  getUserInput(pathStr, current_size, max_size); 
  printf("Please enter the output path of the files: \n");
  getUserInput(outPathStr, current_size, max_size);
  //loop through each file in the directory. Open the file, convert, then close it.
  //we will use dirent.h as it is cross platform so we wont have to worry about sharing issues
  DIR *dir; //new directory
  struct dirent *ent;
  dir = opendir(pathStr); //allcate it a path
  if(opendir(pathStr) == NULL)
  { printf("Error: %d (%s)\n", errno, strerror(errno));}
  int k;

  if(dir != NULL)
  {

         while((ent = readdir(dir)) != NULL) //loop through each file in the directory.
         {
                    //open the file and loop through each line converting it then outputing it into a new file
                if((!strcmp(ent->d_name,"..") || !strcmp(ent->d_name,".")) == 1)
                {
                        //dont want these directories
                        continue;
                }
                else
                {
                sprintf(strIn,"%s%s",pathStr,ent->d_name);    //get the file n
                FILE *fp = fopen(strIn, "r");
                if(fopen(strIn, "r") == NULL) //for inputting file
                { printf("Error: %d (%s)\n", errno, strerror(errno));
                                getchar();
                                  break; }

                sprintf(dirOut,"%s%d%s",outPathStr,counter,".geo");
                printf("%s \n",dirOut);
                FILE *fp2 = fopen(dirOut, "w"); //for outputting file
                if(fopen(dirOut, "w") == NULL)
                     { printf("Error: %d (%s)\n", errno, strerror(errno));
                      getchar();
                      break; }


                     while(fgets(coData, 100, fp) != NULL)//loop through line by line, allocate into 2 doubles and a string, pass the two coordinates and convert
                     {
                        //extract terms from coData                
                        char * pch; //pointer to array pos
                        char * pend;                          
                        pch = strtok(coData," ");                          
                        j = 0;

                            while(j <= 2) //We only want to split the first three parameters.
                            {
                                    //convert char array to double for co-oridinate conversion  

                                    if(j == 0)
                                    {
                                    firstTerm = atof(pch);         //latitude;           
                                    j++;
                                    continue;
                                    }
                                    if(j == 1)
                                    {
                                    pch = strtok(NULL, " ");
                                    secondTerm = atof(pch); //longitude
                                    j++;
                                    continue;
                                    }
                                    if(j == 2)
                                    {
                                    pch = strtok(NULL," ");
                                    elevation = pch; //elevation doesnt need to be converted because it isnt used in the coordinate conversion.
                                    break;
                                    }                                                               
                            }
                       grid2spheroid(&lat,&lon,&convergence,&pt_scale,firstTerm,secondTerm,zone,0, major_axis,flattening,zoneWidth,centMeridian,falseEast,falseNorth,scale);                                            
                       sprintf(strOut,"%f %f %s",lat,lon,elevation);
                       //printf("%d %d", lat, lon);                          
                       fputs(strOut,fp2);


                     }     //end of while        

                      fclose(fp2);
                      fclose(fp);
                      counter++;
                 }

                }
                closedir(dir);
  }
  free(pathStr); //finished using the path string so we can finish the 
  free(outPathStr);
  getchar(); 
  return 0;

 }

  void getUserInput(char *pathStr, int current_size, int max_size)
 {
 unsigned int i = 0;
 if(pathStr != NULL)
  {
              int c = EOF;

              //get the user input and reallocate the memory size if the input it too large.
              while((c = getchar()) != '\n' && c != EOF) //WHILE NOT END OF FILE OR NEW LINE (USER PRESSED ENTER)
              {
                   pathStr[i++] = (char)c;

                   if(i == current_size)
                   {
                        current_size = i+max_size;
                        pathStr = realloc(pathStr, current_size);
                   }    
              }
  }
}
  • 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-07T11:25:15+00:00Added an answer on June 7, 2026 at 11:25 am

    You aren’t closing all the files 😉

    FILE *fp = fopen(strIn, "r");
    if(fopen(strIn, "r") == NULL) //for inputting file
    

    Same applies to your output.

    I think you meant something more like:

    FILE *fp = fopen(strIn, "r");
    if(fp == NULL) //for inputting file
    {
         // error handling.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've found a code which opens this link after logging in to google account
I have 1st popup, and this 1st popup opens 2nd popup Code: <view-state id=paneMaintenance
I tried this code to open a file in Python: f = open("/Desktop/temp/myfile.txt","file1") It
When I open a file using this code if (ofd.ShowDialog() == DialogResult.OK) text =
Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
I have this code, I open a stream (without closing or disposing it), then
I define every thing Already and this code is a part of my code
What i'm trying to do is open every text file in a directory, read
I'm writing this Perl script that gets two command line arguments: a directory and
I have this code: con.Open(); cmd = new MySqlCommand(String.Format(SELECT concat(name,'|',lastname) FROM {0} WHERE ID=

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.