User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 392,042 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,288 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 2775 | Replies: 7
Reply
Join Date: Sep 2003
Location: San Diego, CA, USA
Posts: 75
Reputation: Valmian is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Valmian Valmian is offline Offline
Junior Poster in Training

very strange string behaviur..

  #1  
Sep 13th, 2003
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 *);
)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 1
Reputation: Knightzs is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Knightzs Knightzs is offline Offline
Newbie Poster

Help Problem with function

  #2  
Dec 19th, 2007
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);
}
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 4
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: very strange string behaviur..

  #3  
Dec 19th, 2007
Hello Valmian, I guess you'll receive more help if you use code tags.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote  
Join Date: Dec 2006
Posts: 1,284
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 9
Solved Threads: 88
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: Problem with function

  #4  
Dec 19th, 2007
int clothing(int *, int *, int *, char *[]); /* prototype */
Remove the * in red
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: very strange string behaviur..

  #5  
Dec 19th, 2007
The OP posted this 4 years ago.
Reply With Quote  
Join Date: Mar 2006
Location: Yorkshire, UK
Posts: 2,892
Reputation: happygeek is an unknown quantity at this point 
Rep Power: 8
Solved Threads: 7
Admin
Staff Writer
Featured Blogger
happygeek's Avatar
happygeek happygeek is offline Offline
He's The Daddy

Re: very strange string behaviur..

  #6  
Dec 20th, 2007
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...
Davey Winder
Information Security Journalist of the Year (2008)
www.happygeek.com
Author: Being Virtual
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 4
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: very strange string behaviur..

  #7  
Dec 20th, 2007
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.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: very strange string behaviur..

  #8  
Dec 20th, 2007
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...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 11:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC