Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys,

if you saw my last question, i asked about reading information from files. so far, i have been doing a lot of research and have got something, but it doesn't exactly work too well.

i have a text file called file.txt with a simple letter 'l'. And this is my code for trying to read it:

C++
#include <stdio.h>

int main(int argc, const char **argv[])
{
	FILE *f = fopen("file.txt", "r");
	fseek(f, 0, SEEK_END);
	long pos = ftell(f);
	fseek(f, 0, SEEK_SET);
	
	char bytes[pos];
	fread(bytes, sizeof(char), 1, f);
	fclose(f);
	
	int i = 0;
	for (i = 0; i <= sizeof(bytes); i++)
	{
		printf("%d - %c", bytes[i], bytes[i]);
		printf("\n");
	}
	getchar();
}


what it does is it prints out '108 - l' which is what I want. And then on the next line, it prints out '41 - >'. And then when I try to add more characters to the text file, it just does some more random stuff again. Can someone please tell me what I'm doing wrong and edit my code to show me what I need to do?

thanks a heap,
Fletcher
Posted

Quote:
fread(bytes, sizeof(char), 1, f);
Change to
C
fread(bytes, sizeof(char), pos, f);

(You should also check its return value)


[update]
As pasztorpisti correctly pointed out, you cannot declare a C array that way. You either
  • allocate dynamically the array (using malloc function)
Or
  • Use a very big static array (and make sure your program doesn't overrun it)

[/update]
 
Share this answer
 
v2
Comments
Member 8378691 8-Oct-13 6:26am    
how do I check the return value?
CPallini 8-Oct-13 6:50am    
fread returns the number of items read. Its return value should be equal to pos.
pasztorpisti 8-Oct-13 6:59am    
+5 for the tips - I was lazy to write them :-)
CPallini 8-Oct-13 7:02am    
Thanks (your lazyness :-) ).
C++
char bytes[pos];
fread(bytes, sizeof(char), 1, f);

The size of a C style array must be compile time constant and before closing the file you read in just one byte.
 
Share this answer
 
Comments
CPallini 8-Oct-13 6:43am    
Ouch, I missed that! My 5.
See the MSDN documentation[^] for fread. Also a Google search will find some tutorials that will help you.
 
Share this answer
 
hey guys,

thanks to some help from you and another one of my friends, i finally got some code that works, here it is:

C++
int main(int argc, const char *argv[])
{
	int x;
	int xpos = 0;
	FILE *bin = fopen((const char*)argv[1], "r");
	int length = ftell(bin);
	program = (int*) malloc(length);
	while ((x = (int)getw(bin)) != EOF)
	{
		program[xpos] = x;
		xpos++;
	}
	
	run();
	return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 14-Oct-13 3:09am    
That should not even compile let alone run, and even then the results you get are unlikely to be what you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900