Im trying to open a file, and then read a user defined number of characters from said file. I want to do this using scanf but I can't quite figure out how it works. I can get scanf to work just fine but I can't determine how to specify the maximum number of characters to read.

Have you looked into using ifstream? This is the "new way" to read files where scanf is the "old way".

I haven't I'll do that. Do you mean use get()?

There is a function called getline(), I'm not familiar with just get(). But you can use the >> operator with ifstream. Take a look and let us know when you run into a particular question.

Dave

> I can't determine how to specify the maximum number of characters to read.
scanf() does not offer a user-defined field width like printf(). The way to make it dynamic is with sprintf.

char fmt[15];
char *data = malloc(n);

sprintf(fmt, "%%dc", n);
scanf(fmt, data);

Why not just use a loop and getc()? Better yet, why not use the C++ string class and iostreams instead? They are much more user friendly.

This worked well for me:

ifstream is;
            is.open ("test.txt", ios::binary );
            is.read (buffer,length);
            is.close();
            delete[] buffer;
            if(is.fail())//if an error was encounter before eof...
                top->v0_in = -1;//flag v0_in as negative
            else if (is.eof())
                top->v0_in = 0;//if eof is reached set v0_in to zero
            else
                top->v0_in = is.gcount();//counts the number of characters read before closing