无法理解fgets的行为 [英] Unable to understand the behaviour of fgets

查看:104
本文介绍了无法理解fgets的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我正在阅读process.txt文件。我知道processed.txt有100万行。每一行都是从1到31的所有数字的一些排列。现在,当我尝试在readfile函数中读取文件时,它给出的行数是100万,这是正确的。

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.

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);
}



我正在尝试读取此文件并计算与query.txt相比的反转次数。


I am trying to read this file and calculate the number of inversions compared to a query.txt.

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="">





我尝试过:



现在当我调用getMinInversion函数时,Processed数量达到1000381.我无法理解Processed如何超过100万。除了执行到processed.txt文件的最后一行时,还存在分段错误。我无法弄清楚我的错误。



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.

推荐答案

未选中C语言,这意味着当您访问数组时,它不会检查您是否在阵列。换句话说,只要你在你的记忆空间,你就可以做任何事情。

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.
char line[128];
processed_fp = fopen("processed.txt","r");
while(fgets(line,256,processed_fp))



这在C语言中是完全合法的,但非常危险,因为任何超过127的行都会在分配给的空格后丢失内存。在正在使用之后,空间开始变得很重要,因为之后的变量突然改变了值而没有理由。

您的错误消息是当操作系统检测到您要写入不属于您的内存空间时。



您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。


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.


也许你应该试试这个:



Probably you should try this:

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
}


这篇关于无法理解fgets的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆