Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my code I am reading process.txt file. I know the processed.txt has 1 million lines. Each line is some permutation of all the numbers from 1 to 31. Now when I am trying to read the file in readfile function it gives the number of lines is 1 million which is correct.
C++
void readfile()
{
  FILE * processed_fp;
int no_line = 0;
char line[256];
processed_fp = fopen("processed.txt","r");
 while(fgets(line,256,processed_fp))
   {
     fprintf(stdout,"%s",line);
     no_line ++;
   }
  printf("No of lines read:%d\n",no_line);
 fclose(processed_fp);
}

I am trying to read this file and calculate the number of inversions compared to a query.txt.
C++
int * getMinInversion(int * query)
{
 int curr_inv;
 int * result= malloc(sizeof(int)*10);
 memset(result,-1,sizeof(int)*10);
 int idx = 0;
 char copy_line[256];
 FILE * processed_fp;
 int no_line = 0;
 char line[256];
 processed_fp = fopen("processed.txt","r");

 int * candidate = malloc(sizeof(int)*ARR_SIZE);
 int * subs = malloc(sizeof(int)*ARR_SIZE);
 if (candidate == NULL)
 {
   printf("Could not allocate candidate memory\n");
   exit(EXIT_FAILURE);
 }
 if (subs == NULL)
 {
   printf("Could not allocate substitution memory\n");
   exit(EXIT_FAILURE);
 }
 if (processed_fp == NULL)
 {
   printf("Could not open the processed file\n");
   exit(EXIT_FAILURE);
 }

  while(fgets(line,256,processed_fp))
  {
    strcpy(copy_line,line);
    printf("%s\n",line);
    printf("%s\n",copy_line);
    loadintarray(candidate,copy_line);
    substitution(candidate,query,subs);
    curr_inv=brute_inversion(subs);
    if (curr_inv<min_inv) {="" min_inv="curr_inv;" result[idx]="no_line;" idx++;="" }="" else="" if="" (curr_inv="=" min_inv)="" no_line++;="" printf("processed="" %d\n",no_line);="" fclose(processed_fp);="" return="" result;="" <="" pre="">


What I have tried:

Now when I call the getMinInversion function the Processed number goes upto 1000381. I am not able to understand how can the Processed go above 1 million. Beside when the execution comes to the last line of the processed.txt file there is segmentation fault. I am not able to figure out my mistake.
Posted
Updated 3-Dec-16 4:25am
v2
Comments
Kornfeld Eliyahu Peter 6-Nov-16 6:22am    
Maybe you have lines longer than 256 chars?
Daniel Pfeffer 6-Nov-16 9:41am    
Other things to check:
1. What is the definition of ARR_SIZE? (by your description, it should be at least 31)
2. result is an array of 10 elements. Are you certain that 'idx' cannot go over 9?
3. Check that the 'query' array has the size the you expect it to have.
...
If all else fails, try stepping through the code with a debugger. See if the results are what you expect at each step.
Up_CrossYourHeart 6-Nov-16 20:47pm    
Yes the result of the array was going beyond index 9 that was causing the error.Beside loadintarray had strsep function which was working on the line variable which was also messing up the number of lines that was being read.
Peter_in_2780 6-Nov-16 20:03pm    
The end of your code seems mangled, so I can't read what's going on. I would be tempted to restructure it like this:
...
while(fgets(line,256,processed_fp))
{
do_whatever_you_want_with(line);
no_line ++;
}
...

In other words, decouple your processing of the line from the get-and-count-a-line loop. That will make it a lot easier to see what's going on.
Up_CrossYourHeart 6-Nov-16 20:51pm    
Yes I realized that is why I restructured the code. Right now I have been able to fix the problem. The index in the result was going beyond 9 which was causing the problem. Beside in the loadintarray function the strsep function was working on the line variable which was messing up with the number of lines that was being read from the file.

C language is unchecked, this means that as you access arrays, it don't check that you are in the array. Said otherwise, as long as you are in your memory space, you can do anything.
C++
char line[128];
processed_fp = fopen("processed.txt","r");
while(fgets(line,256,processed_fp))

This is perfectly legal in C but very dangerous as any line longueur than 127 will trash memory after the space allocated to line. It start to matter is space after line is in use, because the variables after line are suddenly changing value without reason.
Your error message is when the OS detect that you are about to write in a memory space that is not yours.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Probably you should try this:

C++
while(fgets(line,256,processed_fp))
{
  strcpy(copy_line,line);
  printf("%s\n",line);
  printf("%s\n",copy_line);
  // Remove processing and try to run this code
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900