![]() |
| | #1 |
| Registered User Join Date: Jul 2007
Posts: 3
| Visual Studio 2005 Debug Assertion Failed ---- Debug Assertion Failed! Program:... File: fgets.c Line: 57 Expression: ( str != NULL ) ---- the really frustrating thing is that this exact same code works on the two other computers i tried to run it on. i have another program that's giving a very similar error except its a (stream != NULL) written next to Expression, and of course the file and line are different because it's a different program. has anybody run into this issue, where the code works fine on other people's computers but not on your own with a very similar error? any idea how to fix (Without re-installing preferably... although if that will definitly work i am certainly open to suggestions) please help! |
| natmas is offline | |
| | #2 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| It's probably an error in the code; perhaps it does something undefined or uses an uninitialized variable or something. It might work on one computer, but that doesn't mean it will work everywhere, as you have seen. Re-installing likely wouldn't help at all. From the name of the source file, perhaps you're passing NULL as the first parameter to fgets()? Of course, it's possible there's something up with your compiler as well. You'd really have to post the code for anyone to tell.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
| | #3 |
| Registered User Join Date: Jul 2007
Posts: 3
| hokay, i'll post the code, because i'm pretty sure there's not an error, but i want to make sure for real before going through the hassle of getting all that stuff off of my computer and back on again... |
| natmas is offline | |
| | #4 |
| Registered User Join Date: Jul 2007
Posts: 3
| Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void textToMatrix(FILE* file, long double *** dest, int* dataRows, int* dataCols){
char delim = ' '; //change for different delimiter
char buf[1024]; //buffer for fgets
printf("Reading File... \n");
char* line = fgets(buf, 1024, file);
// find number of columns of data
int cols = 0;
char *tmp = line; //SHALLOW copy of line
printf("Calculating number of columns... ");
while(*tmp == ' ') { //move over initial white space
tmp++;
}
while(*tmp != '\0') {
if(*tmp == delim){
cols++;
while( *tmp == delim){ //skip extra delimeters
tmp++;
}
} else {
tmp++;
}
}
printf("done! \n");
printf(" cols: %i\n",cols);
// Populate matrix with data from file
printf("Populating matrix with data... \n\n");
long double** data = (long double**)malloc(sizeof(long double *));
int rLen = 0; //Number of rows in data matrix
while( line != NULL) {
//make new row
rLen++;
data = (long double**)realloc(data,rLen*sizeof(long double*));
data[rLen-1] = (long double*)calloc(cols,sizeof(long double));
//populate with data
char** endptr = &line;
for(int i = 0; i<cols; ++i) {
data[rLen-1][i] = (long double)strtod(line, endptr);
//printf("%5f ",data[rLen-1][i]);
}
//printf("\n");
line = fgets(buf,1024,file);
}
printf("done! \n");
printf("rLen: %d\n", rLen);
printf("cols: %d\n", cols);
*dataRows = rLen;
*dataCols = cols;
*dest = data;
}
int main() {
FILE* file = fopen("test.txt","r");
long double** data;
int rows;
int cols;
textToMatrix(file, &data, &rows, &cols);
fclose(file);
printf("rows: %d, cols: %d\n",rows, cols);
for(int i=0; i<rows; ++i) {
for(int j=0; j<cols; ++j) {
printf("%1f ",data[i][j]);
}
free(data[i]);
printf("\n");
}
free(data);
getchar();
return 0;
}
|
| natmas is offline | |
| | #5 | |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Quote:
Code: //populate with data
char** endptr = &line;
for(int i = 0; i<cols; ++i) {
data[rLen-1][i] = (long double)strtod(line, endptr);
//printf("%5f ",data[rLen-1][i]);
}
//printf("\n");
Code: //populate with data
char* endptr = line;
for(int i = 0; i<cols; ++i) {
data[rLen-1][i] = (long double)strtod(endptr, &endptr);
//printf("%5f ",data[rLen-1][i]);
}
//printf("\n");
Also, perhaps you want strtold(). http://www.opengroup.org/onlinepubs/...ns/strtod.html
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort | |
| dwks is offline | |
| | #6 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > From the name of the source file, perhaps you're passing NULL as the first parameter to fgets()? Actually, it's far more likely to be the 3rd parameter, namely the FILE* handle which originates from the UNCHECKED result of fopen()
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #7 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| Especially since a relative path is used and the current directory when running through VC++ isn't necessarily what you'd expect. |
| Daved is offline | |
| | #8 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Yes, that's true -- my guess was before I saw the code, and then I didn't revise it. Checking the return value of fopen() against NULL to see if it failed is a good idea.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Another syntax error | caldeira | C Programming | 31 | 09-05-2008 01:01 AM |
| Help linked lists Visual Studio 2005 | snap_Dragon555 | C++ Programming | 4 | 11-30-2006 04:53 AM |
| using classes | krazykrisi | C++ Programming | 9 | 11-22-2006 10:41 AM |
| Errors with including winsock 2 lib | gamingdl'er | C++ Programming | 3 | 12-05-2005 08:13 PM |
| ras.h errors | Trent_Easton | Windows Programming | 8 | 07-15-2005 10:52 PM |