gpt4 book ai didi

c - gdb 调试 - 仅在 stdin 内\n

转载 作者:太空宇宙 更新时间:2023-11-04 04:22:45 26 4
gpt4 key购买 nike

我正在使用 GDB 在 Windows 中调试我的程序并且需要标准输入。
所以,我编译了它:

gcc -g abstieg2.c
gdb a

break 1
run < graph1.in

但标准输入只有\n !

do{
getline(&line,&size,stdin);
} while(!strcmp("\n",line)); // for testing, gets stuck forever, but only with gdb

我真的看不出还有什么可以与这个问题相关。
谢谢

我的代码的精简版:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <limits.h>

void *errorchecked_malloc(unsigned int);
unsigned int correctinput(char**);
void free_us();
void my_qsort(int , int r);
unsigned int *Dijkstra(int , int , unsigned int , int );
unsigned int min_distance(unsigned int* , int );
int linearsearch(int);
char* strerror(int);

unsigned int *start_edge=NULL; // Input of edges is organised into three array
unsigned int *end_edge=NULL;
unsigned int *length_edge=NULL;
unsigned int *base_camp=NULL ; // Input of basecamps
unsigned int *Dijkstra_dist=NULL; // distance value of dijkstra algorithm
unsigned int *Dijkstra_vis=NULL; // visited by dijkstra algorithm?
unsigned int *Dijkstra_predec=NULL ; // predecessor of dijkstra algorithm
unsigned int* found=NULL;

int * pos_in_vertices;
char* store_for_reset= NULL; // line (input) char* wil be changed, in store_for_reset line will be saved
int basecamp_length=0;

/*Aim: Find the shortest way in a graph from start_vertices to finish_vertices
* restrictions:
* -2 Day travel with max_distance per day
* -after one day a basecamp must be reached( or the finish vertices)
* -N vertices <=N
*
* Input is organized as following:
* start_vertices finish_vertices d_max\n //
* start_vertices end_vertices max_distance\n // for each edge
* ...
* base_camp\n
* base_camp\n
* ...
*/

int main( ) {
int N=0; // count of vertices
int arg_line_count=0; //

unsigned int start; // First three Input of start basecamp
unsigned int end; // finish basecamp
unsigned int d_max; // maximum travel distance per day

char* line = (char *)errorchecked_malloc(36*sizeof(char));
store_for_reset= line;
int size =strlen(line);
//Input configuration
do{
getline(&line,&size,stdin);
printf("%s",line);
} while(!strcmp("\n",line));// for testing, gets stuck forever, but only with gdb
start_edge = (int*)errorchecked_malloc(sizeof(int)*1000); // creating space, TODO dynamic space allocation
end_edge = (int*)errorchecked_malloc(sizeof(int)*1000);
length_edge = (int*)errorchecked_malloc(sizeof(int)*1000);

start = correctinput(&line); // first line input
end = correctinput(&line);
d_max = correctinput(&line);

// input of all edges

for(int i=0;fgets(line,size,stdin);i++){ // fgets returns NULL if stdin empty
printf("Zeile %d \n", i);
start_edge[i]=correctinput(&line);
printf("line: %s", line);
if(line[0]=='\0'){ // end of line, means now are only basecamps left
base_camp[0]=start_edge[i];
start_edge[i]=0;
basecamp_length=1;
break;
}
if(start_edge[i]>N) N= start_edge[i];
end_edge[i]=correctinput(&line);
length_edge[i]=correctinput(&line);
line =store_for_reset;

}




// Input of basecamps
base_camp= (int*)errorchecked_malloc(sizeof(int)*N); // generous, N is maximum of Nodes

for(int i=1;fgets(line,size,stdin);i++){
base_camp[i]=correctinput(&line);
if(line!=NULL){
printf("fatal error:Too many arguments per line while reading \"Basislagern\" input");
free_us();
exit(-1);
}
basecamp_length++;
}
free_us();
}

unsigned int correctinput( char** string){
char* test;
unsigned int tmp =strtol(*string,&test,10);
if((test[0]!=' ' && test[0]!= '\n' ) || errno != 0 ) {
printf("Don't mock me! Please use the correct input format. \n Information: ");
strerror(errno);
printf(" Next Character: \'%d\'", atoi(test));
free_us();
exit(-1);
}
//printf("test: %s, /n",test);
int i;
for(i=0;(*string)[i]>='0' && (*string)[i]<='9';i++){
*string=(*string)+ i*sizeof(char); // moves the input pointer to the next argument( therefore pointer to pointer)
}
if(*string[i]==' ')string++;
return tmp;
}

void free_us(){
free(start_edge);
free(end_edge);
free(length_edge);
free(base_camp);
free(Dijkstra_dist);
free(Dijkstra_vis);
free(pos_in_vertices);
free(Dijkstra_predec);
free(store_for_reset);
free(found);
}

void *errorchecked_malloc(unsigned int size){
void *ptr;
ptr = malloc(size);
if(ptr == NULL) {
fprintf(stderr, "Error: could not allocate heap memory. \n");
free_us();
exit(-1);
}
return ptr;
}

输入看起来像这样:

0 1 3924456639
0 5 1268156980
0 18 293858388
0 74 142402607
1 4 145988610
....
24
1
27
79
4
70
...

最佳答案

您可能遇到了这个 Windows 特定的 gdb 错误:
https://www.cygwin.com/ml/cygwin/1999-04/msg00308.html

尝试升级到最新版本的 gdb(截至目前为 8.0)。在此版本中,除其他外,还对在 MS-Windows 上进行调试进行了一些增强。参见 NEWS file :

  55 * Native debugging on MS-Windows supports command-line redirection
56
57 Command-line arguments used for starting programs on MS-Windows can
58 now include redirection symbols supported by native Windows shells,
59 such as '<', '>', '>>', '2>&1', etc. This affects GDB commands such
60 as "run", "start", and "set args", as well as the corresponding MI
61 features.

关于c - gdb 调试 - 仅在 stdin 内\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44846493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com