Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me remember!

Status
Not open for further replies.

shrubble

MIS
Jul 23, 2003
300
US
I've got a C++ side-job. It's not MFC or .NET, just straight C++ (really almost just C), and I can't remember some really basic stuff that I know I learned in my second semester of C++. Please to help!

Let's say that, instead of using CStrings (or any string data type), we are using char arrays for string storage:

Code:
char myFile[9] = "hello.txt";

(Oh, and we are using the <stdio.h> library for i/o)

and let's also postulate (2-point word!) that we've written a function to take this file name as an argument, but that argument must take the following (pointer) form:

Code:
bool fileExists(char *infile)
{
  //... body of function
}

I cannot, for the life of me, remember how to pass the char array ('myFile[9]') into that function, so that the function has a handle on the file.

Anyone in the mood to point out the incredibly obvious?

"I would rather have a free bottle in front of me, than a pre-frontal lobotomy..."

-Shrubble
 
First of all,
Code:
char myFile[9] = "hello.txt";
shouldn't compile, since "hello.txt" consists of 10 chars (and if you don't know which char the 10th is, I'd say you should re-take that class)


Secondly, it should be a const parameter
Code:
bool fileExists(const char *infile)
{
  //... body of function
}

These things aside, have you tried the obvious?
Code:
if(fileExists(myFile)) { // ...


/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Man- I wrote this at 6:30AM, a long way from home and a compiler... take it easy on me for miscounting!

At any rate, thanks for the response.

Why use const for the argument?

"I would rather have a free bottle in front of me, than a pre-frontal lobotomy..."

-Shrubble
 
Code:
char myFile[9] = "hello.txt";
Is an error in C++ (the string doesn't fit), but is valid in C (though the result is not a string, there is no \0)

If you want to avoid the counting, then
Code:
char myFile[] = "hello.txt";
does exactly what you want it to in both C and C++

If you've no intention of modifying your string, then make it a string constant.
Code:
char *myFile = "hello.txt";

--
 
>take it easy on me for miscounting!

Aye, sorry if I sounded a bit harsh, wasn't my intention.
I think fast, type fast, so pretty pleasy with sugar on top....etc...

>Why use const for the argument?

Well, I'd assume a function like fileExist shouldn't modify the file name.

const is a way to prevent unintentional fiddling with the parameters. Its about dealing with errors sooner (compile time) rather than later (runtime)...

A trivial example:
Code:
void foo1(char* s)
{
  s[0] = 'A'; 
}

void foo2(const char* s)
{
  s[0] = 'A'; // Compile error
}

...
foo1("This will crash!");
foo2("This won't");


/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top