943,712 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3670
  • C RSS
Sep 13th, 2003
0

very strange string behaviur..

Expand Post »
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 <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
#include <stdlib.h>
#include <string.h>

//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<filesize; i++)
data[i] = getc(datafile);
fclose (datafile);
return data;
}
//this function contains the error
short CSVclass::sortdata (char * data)
{
printf ("\ndata:\n%s",data);
if (data == NULL)
return -1;
//finding proportions
arrays.width=1;
for (int i=0; data[i]!='\n'; i++)
{
if (data[i]==',')
arrays.width++;
}
for (int i =0; data[i]!=EOF; i++)
{
if (data[i]=='\n')
arrays.rows++;
}

//finding sizes of strings in the first row
int * namesizes = new int [arrays.width];
{
int n=0;
for (int i=0; data[i]!='\n'; i++)
{
if (data[i]=='\'')
{
int tempsize=0, a=0;
for (a=i+1; data[a]!='\''; a++)
tempsize++;

i=a;
namesizes[n]=tempsize;
n++;
}
}
}
//allocating space
arrays.ar = (CSVarray*)calloc(arrays.width, sizeof(CSVarray));
for (int i =0; i<arrays.width; i++)
{

arrays.ar[i].values=(int*)calloc(arrays.rows,sizeof(int));
arrays.ar[i].name=(char*)calloc(namesizes[i],sizeof(char));

printf ("\nsizeof of the string #%d is:%d",i,sizeof(arrays.ar[i].name));
memset (arrays.ar[i].name, ' ', sizeof(char)*namesizes[i]); //setting strings to a bunch of ' '
printf ("\nThe string when empty is: %s",arrays.ar[i].name);

}
delete [] namesizes; //deleting the sizes of strings from before

//sorting stuff

//names
{
int n=0;
for (int i=0; data[i]!='\n'; i++)
{
if (data[i]=='\'')
{
int a=0;
for (a=i+1; data[a]!='\''; a++)
arrays.ar[n].name[a-i-1]=data[a];
i=a+1;
n+=1;
}
}
}
printf ("\nThe names are:\n");
for (int i =0; i<arrays.width; i++)
printf ("%s :: ", arrays.ar[i].name);
//freeing data and then returning (successfully)
free (data);
return 0;
}
CSVdata CSVclass::CSVload (char * filename)
{
char * data = loadfile (filename);
if (sortdata(data)==0)
{
return arrays;
}
errorC ();
}
CSVdata CSVclass::getcompleetdata()
{
return arrays;
}
"
Here is the .csv file I am loading
"
'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
"
(
as seen from notepad or from simple text loading in
char * CSVclass:loadfile (char *);
)
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
Valmian is offline Offline
82 posts
since Sep 2003
Dec 19th, 2007
0

Problem with function

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);
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Knightzs is offline Offline
1 posts
since Dec 2007
Dec 19th, 2007
0

Re: very strange string behaviur..

Hello Valmian, I guess you'll receive more help if you use code tags.
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Dec 19th, 2007
0

Re: Problem with function

int clothing(int *, int *, int *, char *[]); /* prototype */
Remove the * in red
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Dec 19th, 2007
0

Re: very strange string behaviur..

The OP posted this 4 years ago.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Dec 20th, 2007
-6

Re: very strange string behaviur..

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...
Administrator
Staff Writer
Reputation Points: 1418
Solved Threads: 37
Freelance Word Punk
happygeek is offline Offline
5,569 posts
since Mar 2006
Dec 20th, 2007
0

Re: very strange string behaviur..

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.
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Dec 20th, 2007
0

Re: very strange string behaviur..

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...
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: printing the upper symmetry of a multiplication table
Next Thread in C Forum Timeline: Create Time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC