How To create char with dynamic size? How to remove last symbol of string?

How To create char with dynamic size?

token is random lenght string that holds file path. FindFileData.cFileName is also random lenght string that holds filename.

I nee to create variable str with dynamic name because I want to append file name to it's path, but since I never know string lenght of path or filename I need to create variable str with dynamic size.

I tried:
char str[strlen(token) + strlen(FindFileData.cFileName)];

Error:
1>d:\development\cplusplus\del\del2\del2\del2.cpp(65) : error C2466: cannot allocate an array of constant size 0

And how to remove last char from string? token has * at the end. I need to remove that symbol before I join 2 strings together.

I tried this:
1
2
char *temp;
 strncpy (temp,token,strlen(token)-1);

But it didn't work.

Here's my full code if anyone wants to see it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// del2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "Shlwapi.h"


using namespace std;


int main()
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind;

   //Read list file
   FILE *filein;
   long lSize;
   char * buffer;
   size_t result;
   //Get path to list.txt
   	 char szFileName[MAX_PATH];
     HINSTANCE hInstance = GetModuleHandle(NULL);
     GetModuleFileName(hInstance, szFileName, MAX_PATH);
	 PathRemoveFileSpec(szFileName);
	 PathAppend(szFileName,"\\list.txt");
	 //Open file
   filein = fopen(szFileName, "r");
   //if (filein==NULL) {fputs ("File error",stderr); exit (1);}

    // obtain file size:
    fseek (filein , 0 , SEEK_END);
    lSize = ftell (filein);
    rewind (filein);

    // allocate memory to contain the whole file:
    buffer = (char*) malloc ((sizeof(char)*lSize)+1);

	// copy the file into the buffer:
    result = fread (buffer,1,lSize,filein);
	buffer[result] = '\0'; // make extra byte nul
	/*//Print some info
    printf ("%s\n",buffer);// list.txt
	printf ("%s\n",szFileName);//Path
    fclose(filein);
	*/

	//token
	char *token;
	token = strtok(buffer,"\r\n");
	   while( token != NULL )
       {
           // While there are tokens in "string"
           printf( "%s\n", token );

		   //char *temp;
		   //strncpy (temp,token,strlen(token)-1);
		   //printf("%d\n",strlen(token)-1);
		   /*Str append Example
				char str[strlen(token) + strlen(FindFileData.cFileName)];
				strcpy (str,"these ");
				 strcat (str,"strings ");
				 strcat (str,"are ");
				 strcat (str,"concatenated.");
				 printf("%s\n",str);
			*/
		   //List files
			hFind = FindFirstFile(token, &FindFileData);
			do{
				_tprintf (TEXT("File Found: %s%s\n"),FindFileData.cFileName,token);
			}while(FindNextFile(hFind, &FindFileData));
			FindClose(hFind);

           // Get next token: 
           token = strtok( NULL,"\r\n"); // C4996
       }
	free(buffer);
return	0;
}
rain wrote:
How To create char with dynamic size?

Like this:

1
2
3
char * array = new char[size];
//...
delete[] array;

More info here -> http://cplusplus.com/doc/tutorial/dynamic/

rain wrote:
How to remove last symbol of string?

Put a '\0' in the place of the last symbol.

Though, it would be much easier for you to do the whole thing using std::string to hold the strings and fstream for file i/o.

Info on string -> http://cplusplus.com/reference/string/string/
Info on fstream file i/o -> http://cplusplus.com/doc/tutorial/files/
Last edited on
Thanks.

I got question while I was digging in string pages.

Is there diference between char and string? I found that char is array, but sin't string array too? And is there any diference in speed?
Topic archived. No new replies allowed.