strlen() problem

Hi all,
I am having trouble getting this simple program going.
#include <iostream>
#include <string.h>
#include <fstream.h>

void main()
{
fstream f;
char c;
f.open("f:\\test.txt",ios::in);
while(!f.eof())
{
f.get(c);
cout<<c;
cout<<strlen(c);
}

}

I keep getting an error ;
error C2664: 'strlen' :cannot convert parameter 1 from 'char' to 'const char*'

Its reading from the file no problem, but i cant get the string length to work, I would be grateful for any help with.
Thanks in advance.
there is no fstream.h, change it to <fstream>

also the namespace std is missing
Variable c is a single character. You don't need to calculate its length.
@ Darkmaster
When i don't put the .h after fstream I get an error;
error C2065 'fstream' : undeclared identifier

when I add the " using namespace std" I get an error;

error C2871: 'std' : does not exist or is not a namespace


Im using Microsoft Visual C++ 6.0 fwiw
Last edited on
@ Chervil

I Have a project for my c++ class where i need to be able to read the contents of a file and set up a dynamic array the size of that string. The above code is just me trying this strlen() out. Should I use an array?
Not sure which string you mean.
You mean you want to know the size of the file? Or just the size of the first line? or the first word?

See example code here for file length.
http://www.cplusplus.com/reference/istream/istream/tellg/
I am doing a cash register project, selling 3 items. I save the number in stock of all 3 items to a file as well as the total sales. all 4 are of float type.
An example of the text file looks like this when opened:

10
20
30
50

I need to read these 4 values in and set up a dynamic array for them.
You're on the wrong way. You don't even need to use strlen I guess.
Also you need to include vector. Enough said I hope? No? std::vector<int>.
I have the program working fully without using the dynamic array, Its part of the assignment tough to read in from the file and set up a dynamic array for the size if what's in the file. I am very new to C++ , so I am not sure what you mean when you mention vector.
@ electronicstudent: update your tools to:

Visual Studio 2008 Express:
http://www.microsoft.com/en-us/download/details.aspx?id=3092

Or Visual Studio 2010 Express:
http://go.microsoft.com/?linkid=9709969

Or at least make sure you use the latest Visual Studio 6.0 service pack:
http://www.microsoft.com/en-us/download/details.aspx?id=9183
Ah, so the 'length' you are after is a count of how many lines the file contains.
Indeed, the use of a vector (which is basically an array which can dynamically resize itself as required) sounds like the answer.
http://www.cplusplus.com/reference/vector/vector/
Topic archived. No new replies allowed.