hi! i'm new to the board, (and to C!) and it woud be great if anone could help me out? i've got a program here, and i've been trying to work through it line by line to see what each bit does. i know that the overall gist is to read data from one file with four columns in it, display it on a screen and output it to another file. i know it uses something clled a "linked list", which i believe is a way of managing memory dynamically, but i'm not really sure. anyway, i have attempted to comment every line, and it would be great if anyone could put me right on the stuff i'm a bit hazy about!
thanks so much!
fate
Code:#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) /* begins a function which most of the programm is inside */ { double dummy = 9.0; char line[100], inputfilename[100], outputfilename[100]; /* declares maximum length for variables? */ char*line_ptr; /* creates a pointer to the variable "line" */ struct node /* this is another variable, but it contains variables within itsself? */ { int id; /* these variables that are read in are in the form of a table, this reads the row number (first column)*/ double x, y, z; /* 3 doubles to store values from the other three columns*/ struct node *next; /* now that the four columns have been read, this tells the variable to go to the next row?*/ }; struct node *first_ptr=NULL, *previous_ptr=NULL, new_ptr, *current_ptr=NULL; /* don't really get this bit. these are pointers to something?*/ int no_nodes = 0, no_values; /* setting the initial values to zero? */ FILE*input_stream; /* saying that the input_stream gets its values from an external file? */ FILE*output_stream; /* saying that the output_stream gets its values from an external file? */ fprintf(stdout, "Enter file name for source data:"); /*prompts the user for the name of the data file*/ fscanf(stdin, "%s", inputfilename); /*reads the users response*/ fprintf(stdout, "Enter file name for data to be written to:"); /* prompts the user for an output filename*/ fscanf(stdin, "%s", outputfilename); /* reads the users response */ if ((input_stream = fopen(inputfilename, "r")) !=NULL) /* if the user has given a filename*/ { output_stream = fopen(outputfilename, "a"); /* open the file*/ while(line_ptr = fgets(line, sizeof(line), input_stream) !=NULL) /* while there is information on the next line of the input file*/ { if( no_values = sscanf(line, "%d %lf %lf %lf", &new_ptr.id, &new_ptr.x, &new_ptr.y, &new_ptr.z) ) /* save the values in the file data as an integer, double, double, and double, pointing to the next bit of free memory by the pointers??? */ { current_ptr = (struct node *)malloc(sizeof(struct node)); /* allocate sufficient memory to store a row of data */ *current_ptr = new_ptr; /* no idea whats going on here */ current_ptr->next=NULL; /* no idea whats going on here */ if(first_ptr==NULL) /* no idea whats going on here */ { first_ptr = current_ptr; /* no idea whats going on here */ previous_ptr = current_ptr; /* no idea whats going on here */ } else { previous_ptr->next = current_ptr; /* no idea whats going on here */ previous_ptr = current_ptr; /* no idea whats going on here */ } fprintf(output_stream, "%d,%f,%f,%f\n", current_ptr->id, current_ptr->x, current_ptr->y, current_ptr->z); /*print what has just been read into the output file */ fprintf(stdout, "%d,%f,%f,%f\n", current_ptr->id, current_ptr->x, current_ptr->y, current_ptr->z); /* and into a file */ no_nodes++; /*increment the number of nodes by 1 and do the while loop again */ } if ((line_ptr !=NULL) /* if the line_ptr returns a value not equl to NULL */ && /* logical AND */ (no_values !=4)&&(no_nodes!=0)) /* number of values is not equal to 4, logical AND number of nodes is not equal to 0 */ fprintf(stdout, "Error reading line %s\n", line); /* then the line of data is incorrect. print error reading file on screen */ } if (line_ptr==NULL) /* if the next line is contains no data */ fprintf(stdout, "\nEnd of file found\n"); /* you've reached the end */ current_ptr = first_ptr; /* no idea whats going on here */ while(current_ptr!=NULL) /* while the current pointer is blank*/ { previous_ptr = current_ptr->next; /* no idea whats going on here */ free(current_ptr); /* no idea whats going on here */ current_ptr=previous_ptr; /* no idea whats going on here */ } fclose(output_stream); /*close the output file*/ } return(0); /*return a value of 0*/ } /* fin! */



LinkBack URL
About LinkBacks



i think that if you're ever in loughborough, UK that i owe you a beer or two!