Home > Back-end >  Char type deposit type char array but only in the first character. How full deposit?
Char type deposit type char array but only in the first character. How full deposit?

Time:09-22

 
# include
# include
# include
# include
# include
# define N 3//3 columns
# define L/16000/66000 lines

# define MOVIES "D: \ \ FilmReview \ \ MOVIES dat" 1://movie data: Toy Story (1995) : : Adventure | Animation | Children | Comedy | Fantasy MovieID: : Title: : Genres
# define RATINGS "D: \ \ FilmReview \ \ RATINGS dat"//the user Rating data 1:122: : : : 838985046 UserID: : MovieID: : Rating: : Timestamp
# define MODE "D: \ \ FilmReview \ \ MODE dat"
# define A "D: \ \ FilmReview \ \ a. d. at"
Char data [L] [N]={}
using namespace std;

Void main ()
{
The FILE * fp.
//char (* data) [N].//pointer to an array of
//data=https://bbs.csdn.net/topics/new char [L] [N].
//
//char * data [L];//pointer array
//for (int I=0; i//data [I]=new char [N].

//vector Data (L, vector (N));

//char * * data;
//data=https://bbs.csdn.net/topics/(char * *) malloc (L * sizeof (char *));
//for (int I=0; i//data [I]=(char *) malloc (512 * sizeof (char *));

Int I=0, j=0, count=0;
Char * buf=(char *) malloc (2048 * sizeof (char *));
Char * p=(char *) malloc (2048 * sizeof (char *));
If ((fp=fopen (A, "rb"))==NULL)//open the file
{
Printf (" please make sure the file (% s) whether there is! \ n ", MOVIES);
exit(1);
}
Fseek (fp, 0, SEEK_SET);
While (the fgets (buf, 2048 * sizeof (char), fp)!=NULL)//read in a row
{
Printf (" % s ", buf);
//sscanf (buf, "% % [0-9] : [a-z ([0-9]) :] : : % [a-z]", & amp; Data [count] [0], & amp; Data [count] [1], & amp; Data [count] [2]);//format one line of data stored in arrays
i=0;
P=strtok (buf, ": :");
Data [count] [I]=* p;
While ((p=strtok (NULL, ": :"))) {
Data [count] [+ + I]=* p;
If (I==N - 1)
i=0;
}
Printf (" % s % s % s \ n ", data [count] [0], the data [count] [1], the data [count] [2]).
count++;
}
fclose(fp);
for(i=0; i {
Printf (" % d column element: ", I + 1);
for(j=0; J & lt; N; J++) {
Printf (" % c ", the data [I] [j]);
}
printf("\n");
}

Free (buf);
Free (data);
system("pause");
}


This is my read a file, and then deposited in the array code,
The first picture is my file formats: MovieID: : Title: : Genres
In the second picture is my debug each variable values in operation, and then I found that is in data [count] [I]=* p; , that is, the value of the cut out into an array, it is only the first character in the each cutting point,
The third picture is my figure running results, please you help to look at the great god, how can I cut out the value of the full deposit into an array, at first I thought it was an array of problems, thought that establish a char array, no memory, only the default is sizeof (char *) bytes is 4 bytes, but later found that no matter how much I allocate memory, or the effect, so I also is very big, for several days, try using the map container behind the deposited in the data, but in the end found out is garbled, please everyone a great god's help, this is my another post: http://bbs.csdn.net/topics/392147037 you can also go to see, thank you ~


  • Related