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 . . .

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 *);

Recommended Answers

All 7 Replies

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);
}

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

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

The OP posted this 4 years ago.

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...

commented: Well said. +12

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.

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... :|

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.