using fgets

Is it possible to use fgets function with a dynamic buffer, and not setting its size from the beginning?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  int main()
{
	FILE *FP;
	char* buffer = new char;
	int i = 0;
	char fName[9] = {"data.txt"};
	if((FP = fopen(fName,"rt")) == NULL)
	{
		cout<<"error in opening file";
		return 1;
	}
	if( fgets(buffer,maxRead,FP) != NULL)
	{
		while((buffer[i] >= 65 && buffer[i] <= 90) || (buffer[i] >= 97 && buffer[i] <= 122)  )
		{
			cout<<buffer[i];
			i += 1;
		}
	}
	fclose(FP);
	system("pause");
}


it does prints the content of the file, but it also says that program stopped working.So how can I make work with dynamic buffer? Thanks for the help!
Last edited on
Make sure you allocate a buffer that is at least maxRead bytes big.
Last edited on
Why are you using fgets()? Since this is a C++ program getline() would be more appropriate.

Topic archived. No new replies allowed.