Hi all,
I'm' writing a program in which I can't get round to see what I am doing wrong in a small part of it that deals with:
-dynamically allocating a 1d string array (or 2d char array..)
-reading a file and storing each line in a string (as previously allocated)
I would be grateful if you could direct me in debugging my program. Here's the relevant pieces code:
Code:
#include<stdio.h> /* for input and output */
#include<stdlib.h> /* for memory management */
#include<string.h> /* for memset */
int main(void) {
unsigned int nrows=0; /* number of data rows in the file */
unsigned int comment=0; /* number of comment lines in file */
unsigned int i=0; /* counters */
const unsigned int linelen=700; /* length of buffer line */
FILE *fp=NULL; /* pointer to file to read data */
char oldname[30]; /* name of file to read */
char **buff=NULL; /* buffer line */
char templine[700]; /* temporary line for initial reading of the file */
/* open a file to read */
printf("Enter the name of the file you want to read data from: ");
scanf("%s", oldname);
if (!(fp=fopen(oldname,"r"))) {
perror(oldname);
exit(1);
}
/* count the number of comment lines, and data lines */
while (fgets(templine,sizeof(templine),fp)!=NULL) {
if (templine[0]=='#') {
comment++;
} else {
nrows++;
}
}
printf("\nThere are %u comments and %u rows", comment, nrows);
/* allocate string array to write all the lines from the file */
buff=malloc((comment+nrows)*sizeof(*buff));
if (buff==NULL) {
printf("\nCould not allocate pointer array");
}
for (i=0; i<(comment+nrows); i++) {
buff[i]=malloc(linelen*sizeof(*buff[i]));
if (buff[i]==NULL) {
printf("\nCould not allocate array %u", i);
}
}
/* read all lines into buffer, and print to screen */
rewind(fp);
for (i=0; i<(comment+nrows); i++) {
fgets(buff[i],sizeof(buff[i]),fp);
printf("\n%s", buff[i]);
}
/* clean up memory */
fclose(fp);
for (i=0; i<(comment+nrows); i++) {
free(buff[i]);
}
free(buff);
return 0;
}
EDIT: When I run the code above, I'm getting the result of every three characters to be written to each of the 1d string arrays. This looks like maybe I'm not allocating enough space for the length of my string, even though I want to set it as 700 via the "linelen" variable.
Also, I'm wondering why for array indexing, people generally use "int" and not "unsigned int"? It seems for me that "unsigned int" is more appropriate because of the +ve range being bigger, and because of no need for negative numbers when indexing an array.
Thanks!
Spiros