stringarray

How to create a sort of stringarray ?
I would like to store the available wfd.cFileName in an array.
Later I could address them by their numbers.
This is actually part of a windows program.


#include <windows.h>
#include <iostream>
using namespace std;

int main() {

WIN32_FIND_DATA wfd;
char langarray_filename[200]; // ???;
int langarray_fileposition[200];
int counter=1;
HANDLE File_hnd;
char buffer[50];

//###################
File_hnd=FindFirstFile("languages_menu\\*",&wfd);
// If you compile it, change the path
FindNextFile(File_hnd,&wfd); // .directory
FindNextFile(File_hnd,&wfd); // .. directory - not needed

wsprintf(buffer,"%u %s",counter,wfd.cFileName);
MessageBox(0,buffer,"pos name",MB_OK);

//strcpy(langarray_filename[counter],wfd.cFileName); ???
// This causes:
// [Error] invalid conversion from 'char' to 'char*' [-fpermissive]

langarray_fileposition[counter]=counter;

while (FindNextFile(File_hnd,&wfd))
{
counter++;
wsprintf(buffer,"%u %s",counter,wfd.cFileName);
MessageBox(0,buffer,"pos name",MB_OK);

//strcpy(langarray_filename[counter],wfd.cFileName); ???
// This causes:
// [Error] invalid conversion from 'char' to 'char*' [-fpermissive]

langarray_fileposition[counter]=counter;
}

FindClose(File_hnd);
//###################################

for( int a = 1; a <= counter; a = a + 1) {

wsprintf(buffer,"%u",langarray_fileposition[a]);
MessageBox(0,buffer,"pos",MB_OK);
}


return 1;
}
strcpy(langarray_filename[counter],wfd.cFileName); ???

filename is ONE string. It is an ARRAY of characters. This is C code, or very old C++... use <string> here? Also, for array, use <vector> ?
strcpy(langarray_filename,wfd.cFileName); //this copies wfd to filename, ONE string copied into ONE string. There is no array of strings here.


something like
vector<string> names;
string s;
...
s = wfd.cFileName;
names.push_back(s);
...
names[0] //first one you got, up to names.size() as an array...

you can make names up in your windows object class for this {dialog? or other entity?} to make it available to everything in scope.

I cannot stress enough that you must not use C-style strings if you do not understand them. If you want to use them anyway, stop here, put the windows program aside, and make a little console program with no complexity and use that to learn c-strings until you are confident in what you are doing, THEN come back. You will have nothing but trouble if you try to do this cold without understanding what these things are and how they work. However c++ strings are the way to go, if you don't have a pressing need for C. They hide all the pointer and zero termination details, and things like x=y are easier than strcpy...
Last edited on
jonnin,
thank you.
I didn't mention that I have a very limited idea of c in general.
This is from an old project, that I wrote in assembler, which works fine.
Just for "fun" I am tring to "translate" it into C.
I actually don't want to learn C.

btw. Console program. My example is a console program and compiles fine with dev-cpp.

Is there a possibility to upload zipfiles ? I could upload the whole project, to show taht it works the way I wrote itl.
I can't even make windows programs right now. I have no need, so I have not installed visual studio.

I didn't mention that I have a very limited idea of c in general. No offense, but I noticed.

if you want to do it in C, you need a 2-d array of char. The first dimension is a C-string, an array of char, and the second dimension is an array of that, making it conceptually a 1-d array of c-strings but mechanically a 2-d array of char.
eg
char foop[10][100]; //10 c-strings that can each hold 100 letters (99+ 0 end of string marker).
then you can say strcpy(foop[index], thingy);
and so on.

What I was trying to say is, you need to practice c-strings in a little side-example program. Tying to work them into a gui program without a full understanding is much harder. Then apply what you learn back in the real program. Even if this is a console program, it has a lot of windows junk in it. I was trying to say use a small hello world type example to practice with, no fancy junk in the way.

If you can code assembly, c-strings are similar to assembly: a block of bytes that ends in a zero byte value marker, and you call the functions on them as needed. You only need a small # of the functions:
strlen (length)
strcpy( copy a string)
strcat (append / glue 2 strings together)
strstr(find substring in string, need to understand pointers well to use)
strtok (break a string into parts, eg by spaces)
there isnt much you can't do with those.
Last edited on
" No offense, but I noticed"
Liked that.
Might not believe, but I am a fairly good assembler programmer since 1987.
Last question. If you want to compile a c source, what do you use ?
Like I said, I installed dev-cpp because it behaves quite peacefully and fast.

visual studio seems to be very slow..
all c++ compilers can compile C as far as I know.
a .C extension tells them its a C program.

Visual studio is powerful and slow. Its a tradeoff. I use notepad ++ and g++ these days, but I am not doing anything major in c++ ... my employer does not care for it so we only use it sparingly, mostly as a plugin /workaround to one of our tools or throw-away file fixer type code.

I believe you ... I did a fair bit of assembly in the early 90s, but its fallen off my radar as the need for it has decreased.
Last edited on
jonnin,
thanks again.
Have good one.
Topic archived. No new replies allowed.