- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In order to copy a string, you need to use function strcpy(). Simple assignment like "PlotID
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 |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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()


- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page