-
My Computer Is @#*$!!!
OK, i guess you can tell that im upset!!:p .... For some reason my computer $#%* up when i try to use a char *or a TCHAR *... here
Code:
char *flnm, *str, ch; int i = 0;
cout<<"Enter the file name to be read:"<<endl;
getline(cin, flnm, '<');
ifstream fin(flnm);
while (fin.get(ch)) {
str[i] = ch;
i++;
}
fin.close();
cout<< The file reads..."<<endl<<endl<<str<<endl;
When I run the program it starts up and the computer does its clicking noise a bunch and then it gives me: test has created and error in test.exe, test.exe will now close. and it quits!!!!!!!!!! WHY IS IT DOING THAT!!!!????? Can any1 tell me? I know its the char * thats messing up cause when I comment those lines out (the str[i] = ch; line) it runs through fine (except that i get no return data.:confused: :confused: :confused:
-
flnm and str are wild pointers, point them to buffers or use those directly, ex:
char flnm[100];
-
I know they are... I need an array of chars without knowing the exact number of indexes (an undifined array)... as far as i know there isnt any way to do that except with char *.... am i wrong?
-
number of indexes is rather known as array size, arrays are defined, what you mean is dynamic arrays, and are allocated on the heap, I don't think you need a dynamic array in this case. you'll make it with a buffer of say 255 chars.
-
ok, i'll try ....... I just tried debugging my app when the fatal error appeared... and its coming up with and error on the str variable:
Error: CXX0030: Expression cannot be evaluated.... ???? The str's value is 0xcccccc "". Whats wrong... I know that it works because it works on my other computer.
-
That's usually just a matter of luck. It may work, it may not, but even if it works, it is a very bad thing to do.
Since nobody has filenames longer than 256 characters, it's okay if flnm is a
char flnm[256];
I recommend using a dynamic array for str:
char *str;
// get length of file, don't know how with fstream
int iLength = LengthOfFile(flnm);
str = new char[iLength];
Don't forget to use delete or delete[] when done.
-
What about NTFS? That doesn't have a 260-char limit :D
-
I didn't talk about limits, I talk about practical use. You can extend the array to MAX_PATH to be sure on this side. (You must include windows.h for this constant.)
-
if you dont know how long its gonna be you can use
Code:
char *tmp = new char[]
if you're using VC++ and use strlen(), it'll be the length of the contents (i've tested it, it works, its in that other thread)
-
do you have to use the delete or delete[] function after using the char *tmp = new char[];??
-
I still can't imagine that this gives you valid results...
They may work now, but it could cause problems later...
-
Ok, i am asking which is the best way to do it. I need a way to be able to read and write from some kind of string variable in a way that i can look at one character at a time (just like str[i] = ?). So whats the best kind of variable to use and how do you use it ( i know that the different types of string variables have different functions with them ) What about string (basic_string)?
-
Code:
string whatsit("Hello World");
for(int i = 0; i < whatsit.length(); ++i) {
cout << whatsit[i] << endl;
}
for(string::iterator k = whatsit.begin(); k != whatsit.end(); ++k) {
cout << *k << endl;
}