0

for a program in which I try to create a file and later write into it, I have written the following:

 int main(){ ... .... (some code) .... char DataBuffer[] = "This is the test file"; ... ... } 

I get the error "DataBuffer: undeclared identifier" . I am using Microsoft Visual C++ Express. And in an old asked question here in stackoverflow.com, I have read that Visual C++ uses an old C89 standard and that it does not support C99 standard. For that reason, I must declare the variables at the beginning(which I did for the rest of the parameters of CreateFile() and WriteFile). I mean, when you consider the following:

 DWORD dwCreationDisposition = CREATE_NEW; 

Then I split it up and changed it to:

 DWORD dwCreationDisposition; ... dwCreationDisposition = CREATE_NEW 

but I do not know how I should do it with an array. So, for example when I write:

 char DataBuffer[]; .... DataBuffer[] = = "This is the test file"; 

Then I also get the same error message. What can I do ? Is there any possibility to change the compiler options ? Or a chance to rewrite it such that the integrated compiler accepts it as the other splitted variables/parameters ?

best regards,

3
  • char *DataBuffer; /* ... */ DataBuffer = "string"; Commented Mar 14, 2015 at 10:36
  • You should be able to declare an init the variables in one statement still, albeit at the beginning of the scope. That said, you can also open a new scope using {} in order to declare a new variable there. Note that what @Mint97 suggests is different, because it declares a pointer to non-constant data which you still may not modify, while the original code declared a mutable array of chars. Commented Mar 14, 2015 at 10:38
  • "Undeclared Identifier although it is identified" Did you mean "Undeclared Identifier although it is declared?" Commented Mar 14, 2015 at 10:45

1 Answer 1

2

If you want your string to be re-writable you should so this:

 char DataBuffer[MAX_SIZE]; .... strcpy(DataBuffer,"This is the test file"); 

Also consider using strncpy for avoiding buffer overrun error.

If your string is constant then:

const char DataBuffer[] = "This is the test file"; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.