C++ file I/O
-
Hi! I'm trying to write a small program that analyzes the text in a normal text file. I'm using the C++ ifstream methods. My question is: char *buf; buf = new char; ifstream file("test.txt"); ... file.getline(buf, 100); When I do this buf will contain a row of text. How do I do to know the size of buf?? I've tried sizeof(buf) but I get = 4 Debugging the code I see that buf has more then 4 character...counting them I get to 77. Any good tips! Thanks!
-
Hi! I'm trying to write a small program that analyzes the text in a normal text file. I'm using the C++ ifstream methods. My question is: char *buf; buf = new char; ifstream file("test.txt"); ... file.getline(buf, 100); When I do this buf will contain a row of text. How do I do to know the size of buf?? I've tried sizeof(buf) but I get = 4 Debugging the code I see that buf has more then 4 character...counting them I get to 77. Any good tips! Thanks!
strlen()
function gives the length of a C string, whenSizeof
returns the size in bytes of the variable passed as its parameter... when you writechar *buf;
sizeof(buf);it will always return 4 because on 32-bits architecture, pointers are 32 bits long, ie. 4 bytes...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
Hi! I'm trying to write a small program that analyzes the text in a normal text file. I'm using the C++ ifstream methods. My question is: char *buf; buf = new char; ifstream file("test.txt"); ... file.getline(buf, 100); When I do this buf will contain a row of text. How do I do to know the size of buf?? I've tried sizeof(buf) but I get = 4 Debugging the code I see that buf has more then 4 character...counting them I get to 77. Any good tips! Thanks!
-
char buf[100]; ifstream file("test.txt"); ... file.getline(buf, 100); you should do this I love Programming
-
Hi! I'm trying to write a small program that analyzes the text in a normal text file. I'm using the C++ ifstream methods. My question is: char *buf; buf = new char; ifstream file("test.txt"); ... file.getline(buf, 100); When I do this buf will contain a row of text. How do I do to know the size of buf?? I've tried sizeof(buf) but I get = 4 Debugging the code I see that buf has more then 4 character...counting them I get to 77. Any good tips! Thanks!
Hello, You can retrieve the extraction count. This is the amount of characters read from the last call to an unformatted input function. Look here[^] and here[^] for more information on
gcount()
andbasic_istream
respectively.int nCount = file.gcount();
Hope this helps. Behind every great black man... ... is the police. - Conspiracy brother Blog[^]
-
Hi! I'm trying to write a small program that analyzes the text in a normal text file. I'm using the C++ ifstream methods. My question is: char *buf; buf = new char; ifstream file("test.txt"); ... file.getline(buf, 100); When I do this buf will contain a row of text. How do I do to know the size of buf?? I've tried sizeof(buf) but I get = 4 Debugging the code I see that buf has more then 4 character...counting them I get to 77. Any good tips! Thanks!
Hachaso wrote: file.getline(buf, 100); If you are going to be asking for 100 bytes from the file, the buffer needs to be large enough. As it stands you are only allocating room for 1. Change the allocation to:
buf = new char[100];
Don't forget to delete it when done!
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown