Barry said:
On Sat, 18 Mar 2006 16:04:24 +0100, "Martin Joergensen"
Sorry, your formatting makes it impossible to compile your code and I
didn't spot these in my review.
Okay, forget about this wrapping thing. Guess you're right. I just can't
restrict Visual studio from making lines longer than 65 characters or
so. I went through all the options and help. The following should be
better in that I don't think it inserts ekstra linebreaks in places
never intended to have linebreaks. So what's important is what's inside
main() and I hope that copy/pasting should work - I manually fixed some
comments etc.
- - - - - -- - - - -- -
#include <stdlib.h> /* system("PAUSE") */
#include <stdio.h>
#include <stddef.h>
#include <ctype.h> /* for isspace */
/* defintions */
#define max_input_files 3
/* more prototypes */
void getdata(char *filename, unsigned *pCols, unsigned *pRows, int *count);
int countColsInNextLine(FILE *pFile, unsigned *pCols, unsigned *pRows);
void testwronginput(int c, unsigned cols, unsigned rows);
int main(void)
{
/* read from file and get number of cols+rows */
unsigned count, n_x[max_input_files], n_y[max_input_files];
/* and a lot more var's, not necessary in this example */
double **T, **T_new;
count = 0; /* keep track of number of datafiles read */
getdata("inputfile1.dat", &n_x[0], &n_y[0], &count );
getdata("inputfile2.dat", &n_x[1], &n_y[1], &count );
if(n_x[0] != n_x[1] && n_y[0] != n_y[1])
exit(1);
getdata("inputfile3.dat", &n_x[2], &n_y[2], &count );
if(n_x[1] != n_x[2] && n_y[1] != n_y[2])
exit(1);
double **T = malloc((n_y[0]+1)*sizeof(double*)); /* n_y = rows */
double **T_new = malloc((n_y[0]+1)*sizeof(double*)); /* n_y = rows */
T[0] = malloc((n_x[0]+1)*(n_y[0]+1)*sizeof(double)); /* nx= cols */
T_new[0] = malloc((n_x[0]+1)*(n_y[0]+1)*sizeof(double)); /* nx= cols */
}
void getdata(char *filename, unsigned *pCols, unsigned *pRows, int *count)
{
FILE *pFile;
unsigned rows, cols, oldcols;
printf("Opening file: %s.\n", filename);
if ( (pFile = fopen(filename, "r") ) == NULL)
{
printf("Cannot open file %s.\n", filename);
/* give the user at chance to see this error before the windows
shuts down */
system("PAUSE");
exit(1);
}
*pRows = 0;
*pCols = 0;
/* get nx and ny */
cols = 0;
rows = 0;
/* return oldcols for first row */
if (countColsInNextLine(pFile, &oldcols, &rows) != EOF) {
rows++;
/* check row 2 -> EOF */
while (countColsInNextLine(pFile, &cols, &rows) != EOF) {
rows++;
/* verify that number of cols didn't change */
if (cols != oldcols) {
printf("ERROR: Number of columns is not a constant in
file: %s\n\n", filename);
printf("In line %u, the number of columns is %u
cols.\n", rows, cols);
printf("In the previous line it was counted to
%u.\n\nPlease fix this problem now.\n", oldcols);
exit(1);
}
}
}
if (cols != 0 && cols != oldcols) {
printf("ERROR: Line %u (last line) consisted of %u columns.\n");
printf("Line %u had %u columns.\n", rows, cols, rows-1, oldcols);
exit(1);
}
*pRows = rows; /* update results */
*pCols = oldcols;
(*count)++;
printf("Finished reading from file %s.\n\n", filename);
fclose(pFile); /* close input file, finished reading values in */
}
int countColsInNextLine (FILE *pFile, unsigned *pCols, unsigned *pRows)
{
int c;
unsigned cols = 0;
c = getc(pFile); /* get first character to start the loop */
do {
testwronginput(c, cols, *pRows);
/* first time non-space encountered, update cols */
if (c != EOF && !isspace(c)) {
cols++;
/* skip digits and commas, after first digit */
do {
c = getc(pFile);
testwronginput(c, cols, *pRows);
} while (c != EOF || isspace(c) || c == ',' || c == '.');
/* comma should also be valid input in number */
}
/* skip through blank spaces, but not '\n' ! */
while (c != EOF && c != '\n' && isspace(c)) {
c = getc(pFile);
testwronginput(c, cols, *pRows);
}
} while (c != EOF && c != '\n');
/* is the line finished or is EOF reached? */
*pCols = cols;
return c;
}
void testwronginput(int c, unsigned cols, unsigned rows)
{
/* I tried isblank() but my system complained about it */
if (isalpha(c)) {
printf("Error! Alphabetic character '%c' encountered before
EOF\n", c);
printf("Last succesful location read was: Line %u, Column
%u\n", 1+rows, cols);
/* give the user at chance to see this error before the windows
shuts down */
system("PAUSE");
exit(1);
}
if (iscntrl(c) && c != 10) {
printf("Error! Control character '%c' encountered before
EOF\n", c);
printf("Last succesful location read was: Line %u, Column
%u\n", 1+rows, cols);
/* give the user at chance to see this error before the windows
shuts down */
system("PAUSE");
exit(1);
}
if (!isprint(c) && c != EOF && c != '\n') {
printf("Error! Non-printing character '%c' encountered before
EOF\n", c);
printf("Last succesful location read was: Line %u, Column
%u\n", 1+rows, cols);
/* give the user at chance to see this error before the windows
shuts down */
system("PAUSE");
exit(1);
}
}
- - - - - -- - - - -- -
Best regards / Med venlig hilsen
Martin Jørgensen