我正在尝试读取带有空格分隔边的图形。有一个问题从char *缓冲区解析到整数。
示例图:
1 2
1 3
2 3
3 4
4 5
1 5
5 6
1 6
输出:
first edge: 1
second edge: 2
first edge: 0
second edge: 0
first edge: 1
second edge: 3
first edge: 0
second edge: 0
first edge: 2
second edge: 3
first edge: 0
second edge: 0
first edge: 3
second edge: 4
first edge: 0
second edge: 0
first edge: 4
second edge: 5
first edge: 0
second edge: 0
first edge: 1
second edge: 5
first edge: 0
second edge: 0
first edge: 5
second edge: 6
first edge: 0
second edge: 0
first edge: 0
second edge: 0
first edge: 1
second edge: 6
first edge: 0
second edge: 0
first edge: 0
second edge: 0
显然所有那些零都不应该存在。
代码:
#include <stdio.h>
#include <math.h>
#include <ctype.h>
int main(int argc, char* argv[]){
FILE *fin;
fin = fopen(argv[1], "r");
int n_e = 0;
char* buffer = (char*)malloc(2048);
char* n1 = (char*)malloc(256);
char* n2 = (char*)malloc(256);
int v_1 = 0;
int v_2 = 0;
int flag = 0;
while(!feof(fin)){
int bytes_read = fread(buffer, 1, 2048, fin);
for(int i = 0; i < bytes_read; i++){
if(isdigit(buffer[i])){
if(flag == 0){
n1[v_1++] = buffer[i];
}
else
n2[v_2++] = buffer[i];
}
else if(buffer[i] == ' ')
flag = 1;
else{ //end of the line??
n_e++;
flag = 0;
n1[v_1++] = '\0';
n2[v_2++] = '\0';
int first = atoi(n1);
int second = atoi(n2);
printf("first edge: %d\n", first);
printf("second edge: %d\n", second);
v_1 = 0;
v_2 = 0;
}
}
}
return 0;
}
答案 0 :(得分:1)
您可以在一行中获得所需的结点:fscanf
行。其余的用于框架和错误检查 - 这是任何程序的重要组成部分。将图形边缘作为文本文件:
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *fin;
int first, second;
if(argc < 2) { // check # arguments
return 0; // or other action
}
fin = fopen(argv[1], "rt");
if(fin == NULL) { // check the file opened
return 0; // or other action
}
while(fscanf(fin, "%d%d", &first, &second) == 2) { // check the number of items read
printf("first edge: %d, second edge: %d\n", first, second);
}
fclose(fin);
return 0;
}
节目输出:
first edge: 1, second edge: 2
first edge: 1, second edge: 3
first edge: 2, second edge: 3
first edge: 3, second edge: 4
first edge: 4, second edge: 5
first edge: 1, second edge: 5
first edge: 5, second edge: 6
first edge: 1, second edge: 6
另请注意,feof
未按照您的方式使用,如上所述。
答案 1 :(得分:0)
您可以检查sscanf()
函数返回的值,以确定是否已从该行成功解析样本。
我重写了代码以演示解决问题的更有效方法:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LEN (32)
#define MAX_EDGES_SAMPLES (256)
/* Represents an Edge Object */
typedef struct edge_s
{
int first;
int second;
} edge_t;
int main( int argc, char * argv[] )
{
FILE * pf = NULL;
char line[ MAX_LINE_LEN + 1 ] = {0};
int count = 0;
int i = 0;
edge_t edges[ MAX_EDGES_SAMPLES ]; /* Edges array */
/* Initialize Edges array */
memset( &edges, 0, sizeof( edges ) );
/* Open input text file for reading */
pf = fopen( argv[1], "r" );
if(!pf)
{
printf("Error openning file: %s\n", argv[1] );
return 1;
}
/* For each line... */
while( fgets( line, MAX_LINE_LEN, pf ) )
{
edge_t aux;
/* Parse Line */
int ret = sscanf( line, "%d %d", &aux.first, &aux.second );
/* Ignore malformed lines */
if( ret != 2 )
continue;
/* Add values to the Edges array */
edges[count].first = aux.first;
edges[count].second = aux.second;
/* Increment Edges Count */
count++;
if( count >= MAX_EDGES_SAMPLES )
break;
}
/* Free resource */
fclose(pf);
/* Display Edges array data */
for( i = 0; i < count; i++ )
printf("Edge[%d]: first=%d second=%d\n", i, edges[i].first, edges[i].second );
return 0;
}
/* eof */
希望它有帮助!