954,167 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

very strange string behaviur..

Well hello.. I am making an CSV (comma seporated values) loader, in which the first row are strings and all the other are integers. From what I've got when I load each indivisual string into an individual char array first few char arrays when printed are twice as big as they should be and the output is crazy for those arrays.
Here is the output (under data is the .csv file I am using (after I load it)):
"
This is Target Modeler version 1.0.0
data:
'Y0-1','Y1-1','Y2-1','Y0-2','Y1-2','Y2-2','Y0-3','Y1-3','Y2-3'
-996,-589,-181,194,414,431,762,910,817
-997,-587,-186,192,414,432,758,904,819
-1000,-585,-186,194,413,433,769,909,815
-999,-591,-182,194,414,434,800,913,815
-1000,-590,-184,192,415,431,803,903,815
-1002,-592,-189,194,414,434,772,904,815
-999,-594,-192,195,418,434,780,906,814
-1004,-594,-193,197,418,434,772,905,815
-1003,-595,-193,195,415,435,759,905,821
-1005,-596,-194,195,415,437,809,906,825
-1006,-598,-195,196,417,436,770,905,824
-1006,-600,-193,197,418,438,757,906,819
-1006,-600,-199,198,417,435,756,906,817
-1008,-601,-199,198,418,436,759,906,817
-1009,-601,-200,198,418,437,759,908,817
-1011,-603,-201,198,418,436,757,909,814
-1011,-604,-202,199,419,437,757,908,816
sizeof of the string #0 is:4
The string when empty is: ta:
sizeof of the string #1 is:4
The string when empty is:
sizeof of the string #2 is:4
The string when empty is:
sizeof of the string #3 is:4
The string when empty is:
sizeof of the string #4 is:4
The string when empty is:
sizeof of the string #5 is:4
The string when empty is:
sizeof of the string #6 is:4
The string when empty is:
sizeof of the string #7 is:4
The string when empty is:
sizeof of the string #8 is:4
The string when empty is:
The names are:
Y0-1ta: //ta:\n are the extra 4 bytes (comment not from output)
:: Y1-1 :: Y2-1 :: Y0-2 :: Y1-2 :: Y2-2 :: Y0-3 :: Y1-3 :: Y2-3 ::
Press any key to continue . . .
//from system("PAUSE");(comment not from output)

"
Here is the source code (error is probably in
short CSVclass::sortdata (char * data);
inside CSVloader.cpp):
main.cpp
"
#include
#include
#include "CSVloader.h"
#define _VERSION "1.0.0"
CSVclass td;
int main (void)
{
printf ("This is Target Modeler version %s\n", _VERSION);
CSVdata sdata = td.CSVload("tipical.csv");
fflush (stdout);
puts("");
system ("PAUSE");
return 0;
}
"
CSVloader.h
"
//loads data.. initial processing
#ifndef _LOADER_H
#define _LOADER_H
//libraries
#include
#include
#include

//classess
class CSVarray
{
public:
char *name;
int *values;
CSVarray(){name=NULL;values=NULL;}
~CSVarray(){if (name!=NULL) free (name); if(values!=NULL) free (values);}
};
class CSVdata
{
public:
CSVarray * ar;
int width;
int rows;

CSVdata() {ar=NULL; width=0; rows=0;}
~CSVdata() {if (ar!=NULL) free (ar);}
};
class CSVclass
{
private:
CSVdata arrays;
char * loadfile (char *);
short sortdata (char *);
void errorC() {printf ("\nUnidenterfied error,aborting program\n"); abort();}
public:
CSVdata getcompleetdata();
CSVdata CSVload (char *);
CSVclass() {} //empty
~CSVclass(){} //empty
};

#endif
"
CSVloader.cpp
"
#include "CSVloader.h"
char * CSVclass::loadfile (char * filename)
{
FILE * datafile = NULL;
if ((datafile=fopen (filename, "r"))==NULL)
{
printf ("\nError - no file found");
return NULL;
}
fseek (datafile,0,SEEK_END);
int filesize = ftell (datafile);
char * data = NULL;
data = (char*)calloc (filesize,sizeof (char));
rewind (datafile);
for (int i=0; i

Valmian
Junior Poster in Training
82 posts since Sep 2003
Reputation Points: 13
Solved Threads: 0
 

The compiler compiles, but when it comes to statement 4, an error message comes "General Protection Exception".. i havent put the function because its very long, so i would like to know whether it is possible to hav this function.

int clothing(int *, int *, int *, char *[]); /* prototype */

main()
{
int rate;
char clothdes[40];

rate=clothing(&clothtype, &outfittype, &size, clothdes); /* statement 4 */

puts(clothdes);
}

Knightzs
Newbie Poster
1 post since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

Hello Valmian, I guess you'll receive more help if you use code tags.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

int clothing(int *, int *, int *, char *[]); /* prototype */
Remove the * in red

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

The OP posted this 4 years ago.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

Indeed.

A quick reminder folks - please do not bump threads that are this old, and certainly not with unrelated questions that should be asked in their own right (this is for your own good as much as ours - you stand mopre chance of getting help that way.)

Talking of which, please use code tags as this also makes it a lot easier for people to help you with your code problems.

Many thanks...

happygeek
Freelance Word Punk
Administrator
27,458 posts since Mar 2006
Reputation Points: 1,457
Solved Threads: 55
 

Oops, I didn't see that. I saw that the last post made by Knightzs was recent and so replied. I will have to be careful from now onwards.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

Yeah, I've done the same thing. Now whenever I see a thread that's "on fire" with a zillion views and only a few replies the first thing I look at is the age of the thread, then I look to see if the latest poster is adding value... :|

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You