I'm trying to do a DFS search on a given graph using the following code:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
typedef struct G{
int vertex1;
int vertex2;
float num;
} graph;
typedef struct adj{
std::vector<int> element;
}adj;
void dfs (int v, bool marked[], adj*p){
marked[v]=true;
std::vector<int>::iterator i;
for (i=p[v].element.begin(); i!=p[v].element.end();i++){
if (!marked[*i]){
dfs(*i, marked, p);
}
}
}
void Search(adj*p, int*tvertex){
bool *marked=new bool[*tvertex];
for (int v=0; v<*tvertex; v++){
marked[v]=false;
}
for (int v=0; v<*tvertex;v++){
if (marked[v]==false){
dfs(v, marked,p);
}
}
}
void buildadj(graph*g, adj*p, int * tvertex, int *edge ){
for (int e=0; e<*edge; e++){
p[g[e].vertex1].element.push_back(g[e].vertex2);
p[g[e].vertex2].element.push_back(g[e].vertex1);
}
}
void readInData(FILE *fp, graph*g, int *tvertex) {
char buffer[500];
char *token;
const char delimiters[] = " ";
int i;
int n;
memset(buffer, 0, 499);
for(i = 0;!feof(fp);) {
i++;
if (i>=2){
fscanf(fp, " %[^\n]", buffer);
token = strtok(buffer, delimiters);
n = (int) atoi(token);
g[i-2].vertex1 = n;
g[i-2].vertex2 = (int) atoi(strtok(NULL, delimiters));
g[i-2].num = (float)atof(strtok(NULL, delimiters));
}
}
}
void readstrct(FILE *fp,int*edge, int*tvertex){
int i;
int a[2];
while (EOF!=fscanf(fp, "%d\n", &a[i])) {
i++;
if(i>=2){
break;
}
}
*tvertex=a[0];
*edge=a[1];
}
void sendMessage() {
char message[200];
sprintf(message, "Wrong Format\n");
printf("%s", message);
}
int main(int argc, char * argv[]) {
FILE *fp;
int edge;
int tvertex;
if(argc < 2) {
printf("File not given\n");
sendMessage();
return 0;
}
fp=fopen(argv[1], "r");
if(fp == NULL) {
printf("file not found\n");
sendMessage();
return 0;
}
readstrct(fp,&edge, &tvertex);
graph *g=new graph[edge];
adj *p=new adj[tvertex];
readInData(fp, g, &tvertex);
buildadj(g,p,&tvertex, &edge);
Search(p,&tvertex);
}
The input is of the following form:
Please focus on the readstruct and readIndata function where the data is read
and stored. I intend to read the first two lines (13 and 13) and stores these
two values as edges and vertex num in the ' readstruct ' function.
After reading line 1 and line 2, the if condition is satisfied and goes out
of loop.
Line 3 and Line 15 are read in the readIndata when i larger than 1
Since per-line contains three data and space between them, I used char array
to store them and read one character by one character. The Segmention faul
error shows when the code continues to read the file when it reaches line
15(or i=13) in readIndata function, where it should stop. So I guess
something messed up with filepointer or fscanf function.
Best
You must first initialize i
in readstrct()
. As the post reads now, it is used as an index into the integer array, a
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.