Im currently writing a c++ program that needs to extract string and numeric data from a text file. The format of the data is the following;
3225 C9+ ELECTR C8 C * 1.00E-6 -0.30 0.0
first entry is an integer, next 5 entries are strings and the last 3 are floats. No string is ever greater than 7 characters long.
I am reading the file line by line and then extracting the data using;
sscanf(ln.c_str(),"%d %s %s %s %s %s %e %e %e",
&rref[numre],&names[numre][0],&names[numre][1],&names[numre][2],&names[numre][3],
&names[numre][4],&nums[numre][0],&nums[numre][1],&nums[numre][2]);
this works fine untill I meet a line like;
3098 SIC2H3+ ELECTR SIC2H2 H * 1.50E-7 -0.50 0.0
where one of the entrys is the full 7 characters long. In this case I get;
names[3097][0] = "SIC2H3+ELECTR"
and,
names[3097][1] = "ELECTR"
Anybody got any ideas…they will be much appreciated!!
The most likely problem is in the declaration of
names: if you declared it as holding seven characters or less, and forgot to allocate space for terminating zero, you’d get the results that you are describing.will have enough space for strings of length 6 or less; for strings of length 7, you need