当我的文件指针第二次倒带时,它会导致段错误。我不知道为什么。我将在顶部包含有问题的 main 以及它下面的所有代码。
int main(void){
// creating the file pointer
FILE *fptr = fopen("input-machine-problem-1.txt", "r");
if (fptr == NULL) {
printf("Error! opening file");
return 0;
}
int edges;
int vertices;
int* edgesPtr = &edges;
int* verticesPtr = &vertices;
getNumberOfVerticesAndEdges(fptr, verticesPtr, edgesPtr);
LinkedList arrayOfLinkedLists[vertices];
int x, y;
for(int i = 0; i < vertices; i++){
if(fptr == NULL){
return 0;
}
rewind(fptr);
for(int j = 0; j < edges; j++){
printf("%d: ", j);
fscanf (fptr, "%d %d", &x, &y);
printf ("%d %d ", x, y);
if(i == x){
push(arrayOfLinkedLists[i], y);
} else if(i == y){
push(arrayOfLinkedLists[i], x);
}
printf("**\n");
}
}
// printAdjacencyLists(arrayOfLinkedLists, vertices);
fclose (fptr);
}
如果您想复制和粘贴整个文件:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int vertexNumber;
struct node *next;
} Node;
typedef struct linkedList{
Node* head;
int size;
} LinkedList;
void getNumberOfVerticesAndEdges(FILE* fp, int* vertices, int* edges);
void push(LinkedList linkedList, int vertex);
Node* createNode(int vertexNumber);
void printAdjacencyLists(LinkedList* arrayOfLinkedLists, int vertices);
int main(void){
// creating the file pointer
FILE *fptr = fopen("input-machine-problem-1.txt", "r");
if (fptr == NULL) {
printf("Error! opening file");
return 0;
}
int edges;
int vertices;
int* edgesPtr = &edges;
int* verticesPtr = &vertices;
getNumberOfVerticesAndEdges(fptr, verticesPtr, edgesPtr);
LinkedList arrayOfLinkedLists[vertices];
int x, y;
for(int i = 0; i < vertices; i++){
if(fptr == NULL){
return 0;
}
rewind(fptr);
for(int j = 0; j < edges; j++){
printf("%d: ", j);
fscanf (fptr, "%d %d", &x, &y);
printf ("%d %d ", x, y);
if(i == x){
push(arrayOfLinkedLists[i], y);
} else if(i == y){
push(arrayOfLinkedLists[i], x);
}
printf("**\n");
}
}
// printAdjacencyLists(arrayOfLinkedLists, vertices);
fclose (fptr);
}
void push(LinkedList linkedList, int vertex){
Node* newNode = createNode(vertex);
Node* cur = linkedList.head;
Node* prev = cur;
if(cur == NULL){
linkedList.head = newNode;
return;
}
while(newNode->vertexNumber > cur->vertexNumber){
prev = cur;
cur = cur->next;
}
newNode->next = cur;
prev->next = newNode;
}
Node* createNode(int vertexNumber){
Node* newNode = malloc(sizeof(Node));
if(!newNode){
return NULL;
}
newNode->vertexNumber = vertexNumber;
newNode->next = NULL;
return newNode;
}
void getNumberOfVerticesAndEdges(FILE* fp, int* vertices, int* edges){
if (fp == NULL) {
printf("Error! opening file");
return;
}
*vertices = 0;
*edges = 0;
while(1){
int x, y;
fscanf(fp, "%d %d^\n", &x, &y);
if(x > *vertices){
*vertices = x;
} if (y > *vertices){
*vertices = y;
}
*edges = (*edges) + 1;
if(feof(fp)) {
return;
}
}
}
void printAdjacencyLists(LinkedList* arrayOfLinkedLists, int vertices){
for(int i = 0; i < vertices; i++){
printf("\n%d: ", i);
if(arrayOfLinkedLists[i].head == NULL){
return;
}
Node* cur = arrayOfLinkedLists[i].head;
while(cur != NULL){
printf("%d --> ", cur->vertexNumber);
cur = cur->next;
}
}
}