Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Beginner's problem with strings

michael_green
Beginner
473 Views
Hi All,

I want to load an array of strings with data read from a file but the methods I am used to from other languages just don't work in C/C++. Please could someone explain my misunderstandings and show me what to do.

Here's an extract of what I've got:

#include
...

char* get_csvstring(char* instring, char* outstring);

string PlotID[3100];
char temp[10];
char Buffer[30];
FILE *fptr;
char *tp;
int k;

...

//Open plot file
if((fptr = fopen("\\\\My Documents\\\\JI-gnd-plots.csv","r")) != NULL)
{
fgets(Buffer,30,fptr);
k = 0;
while(!feof(fptr))
{
tp = get_csvstring(Buffer,temp);
PlotID = temp;
k++;
fgets(Buffer,30,fptr);
}
}

The function get_csvstring collects the first 9 characters of Buffer and returns them as temp. The routine runs, but contrary to my original expectations, all elements of the array PlotID end up taking the same value, the last encountered. I think I understand now why this is so, but my real question is how can I acquire an array of different string values from the data file? I have tried many variations on the above without success.

With many thanks in advance for any help or suggestions,
Mike.
0 Kudos
8 Replies
Feilong_H_Intel
Employee
473 Views
Mike,

In order to copy a string, you need to use function strcpy(). Simple assignment like "PlotID = temp" won't work.

Should do something like this:

strcpy(PlotID + k, temp, 9);
k += 9;

Thank you.
--
Feilong H.
Intel Developer Support

Tools Knowledge Base: http://software.intel.com/en-us/articles/tools
0 Kudos
TimP
Honored Contributor III
473 Views
You have defined temp as a pointer to a string. You can copy it to another pointer to a string, but it's still a pointer to a string. 35 years later, people are still inventing new C++ namespaces, but as they come and go C stays the same. This is an example of what is wrong with the term "C/C++"
0 Kudos
mecej4
Honored Contributor III
473 Views
>This is an example of what is wrong with the term "C/C++"

How true! There have been numerous instances where posted problematic code segments could have been valid in both C and C++, but worked quite differently depending on whether the source file was *.c or *.cpp. Users often think that since their compiler has a name containing C++ that the programs that they write in C and get compiled without complaint by the compiler must be valid C++ sources.
0 Kudos
michael_green
Beginner
473 Views
Hi Guys,

Thanks for replies. I had already tried the strcpy function (and endless variations around that theme) but all I ever get is compiler errors. I am actually writing for a handheld PC running Windows CE, so all my strings/characters/functions refer to Unicode characters - althoughI don't think that should make any difference. Here's my latest effort, with the compiler errors:

wstring wPlotID[3100];
wchar_t wtemp[10];
...
wcscpy(wPlotID + k,wtemp);
k+= 9;

1>.\WQA.cpp(344) : error C2664: 'wcscpy' : cannot convert parameter 1 from 'std::wstring *' to 'wchar_t *'

1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

1>.\WQA.cpp(364) : error C2664: 'wcscpy' : cannot convert parameter 2 from 'std::wstring' to 'const wchar_t *'

1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Any further insights greatly appreciated,
Many thanks
Mike

0 Kudos
gpseek
Beginner
473 Views
Quoting michaelgreen
wstring wPlotID[3100];
wchar_t wtemp[10];
...
wcscpy(wPlotID + k,wtemp);
k+= 9;

1>.\WQA.cpp(344) : error C2664: 'wcscpy' : cannot convert parameter 1 from 'std::wstring *' to 'wchar_t *'

1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

1>.\WQA.cpp(364) : error C2664: 'wcscpy' : cannot convert parameter 2 from 'std::wstring' to 'const wchar_t *'

1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


I dont think it is a good idea to use both wstring and wchar_t for such a simple function. One of them is enough. If you want to use wscpy, you simply declarewPlotID as wchar_t too, which makes your world a little bit cleaner.

You might have other problems down the road too. You have to make sure your data file is really a unicode file. Otherwise, you need to do conversions between ansi and unicode.


0 Kudos
michael_green
Beginner
473 Views
Thanks for reply. I have tried this too but I always get into trouble. For example, I presume I need to declare an array of some sort in which to place my strings, and from what I read about the subject the following looks appropriate:

wchar_t wPlotID[10][3100];

This compiles OK, but even if I don't do anything with it (other than declare it) the program just hangs - no error messages, just an apparent infinite loop of some sort. At any rate, I have to crash the program to regain control.

Many thanks again for any help.
Mike
0 Kudos
gpseek
Beginner
473 Views
Quoting michaelgreen
wchar_t wPlotID[10][3100];


It seems to me that it should be
wchar_t wPlotID[3100][10]; where 3100 is number of rows

As I said before, you have to make sure your data file is really in UNICODE format, which is usally not.
I did something similar on a WinCE device in a project, parsing an ANSItext file and storing the data to a database.

So, be aware that wchar_t and char are different. Conversions are needed.
Check out this Windows API: MultiByteToWideChar()

0 Kudos
gpseek
Beginner
473 Views
Quoting michaelgreen
wchar_t wPlotID[10][3100];


It seems to me that it should be
wchar_t wPlotID[3100][10]; where 3100 is number of rows

As I said before, you have to make sure your data file is really in UNICODE format, which is usally not.
I did something similar on a WinCE device in a project, parsing an ANSItext file and storing the data to a database.

So, be aware that wchar_t and char are different. Conversions are needed.
Check out this Windows API: MultiByteToWideChar()

0 Kudos
Reply