Click here to Skip to main content
15,904,935 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: CListCtrl Notify msg Pin
Michael Dunn29-Jun-06 15:47
sitebuilderMichael Dunn29-Jun-06 15:47 
Questionfile manipulation with fopen(), fscanf() Pin
kitty529-Jun-06 10:06
kitty529-Jun-06 10:06 
AnswerRe: file manipulation with fopen(), fscanf() Pin
David Crow29-Jun-06 10:16
David Crow29-Jun-06 10:16 
GeneralRe: file manipulation with fopen(), fscanf() Pin
kitty529-Jun-06 10:25
kitty529-Jun-06 10:25 
QuestionRe: file manipulation with fopen(), fscanf() Pin
David Crow29-Jun-06 11:32
David Crow29-Jun-06 11:32 
AnswerRe: file manipulation with fopen(), fscanf() Pin
kitty529-Jun-06 11:37
kitty529-Jun-06 11:37 
GeneralRe: file manipulation with fopen(), fscanf() Pin
David Crow30-Jun-06 2:14
David Crow30-Jun-06 2:14 
AnswerRe: file manipulation with fopen(), fscanf() [modified] Pin
Zac Howland29-Jun-06 10:32
Zac Howland29-Jun-06 10:32 
You actually have a couple problems:

kitty5 wrote:
//initialize data
for ( int a = 0; a < 200; a++ )
{
data[a] = "q";
}


Besides being inefficient, this is actually buggy. The reason is simple: You have an array of 200 unallocated strings. You are marching through all 200 and setting each one to "q\0". Since there is no memory allocated for these pointers ... you are writing in random places (NOT GOOD!).

kitty5 wrote:
fscanf(fpread, "%s", &data[i]); <- problem is here... i think


Don't use %s with scanf functions. In most cases, it won't do what you hope it will. Instead, use fread, or fgetc. A slightly better solution would be to read in the entire buffer at once (decreasing your harddrive accesses):

int _tmain(int argc, _TCHAR* argv[])
{
	char *data[200] = {0};	// initialize it here
	char* buffer = NULL;		// buffer for file
	FILE *fpread;
	int i = 0;

	if ((fpread = fopen("data.txt", "r")) == NULL)
	{
		printf("Cannot open file for reading. \n");
		return -1;
	}

	if (0 != fseek(fpread, 0, SEEK_END))
	{
		printf("Cannot seek end of file.\n");
		fclose(fpread);
		return -1;
	}

	size_t fileSize = ftell(fpread);
	if (0 != fseek(fpread, 0, SEEK_SET))
	{
		printf("Failed to seek beginning of file.\n");
		fclose(fpread);
		return -1;
	}

	buffer = new char[fileSize + 1];
	memset(buffer, 0, fileSize + 1);
	if (fileSize != fread(buffer, sizeof(char), fileSize, fpread))
	{
		printf("Failed to read file.\n");
		fclose(fpread);
		delete [] buffer;
		buffer = NULL;
		return -1;
	}

	// parse buffer here

	delete [] buffer;
	buffer = NULL;
	return 0;
}




If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

Zac

-- modified at 16:33 Thursday 29th June, 2006
GeneralRe: file manipulation with fopen(), fscanf() Pin
kitty529-Jun-06 10:55
kitty529-Jun-06 10:55 
GeneralRe: file manipulation with fopen(), fscanf() Pin
Zac Howland29-Jun-06 11:06
Zac Howland29-Jun-06 11:06 
GeneralRe: file manipulation with fopen(), fscanf() Pin
kitty529-Jun-06 11:19
kitty529-Jun-06 11:19 
GeneralRe: file manipulation with fopen(), fscanf() Pin
Zac Howland29-Jun-06 15:57
Zac Howland29-Jun-06 15:57 
QuestionRe: file manipulation with fopen(), fscanf() [modified] Pin
kitty529-Jun-06 11:55
kitty529-Jun-06 11:55 
AnswerRe: file manipulation with fopen(), fscanf() Pin
Zac Howland29-Jun-06 15:59
Zac Howland29-Jun-06 15:59 
AnswerRe: file manipulation with fopen(), fscanf() Pin
Stephen Hewitt29-Jun-06 18:01
Stephen Hewitt29-Jun-06 18:01 
QuestionStatic Variables [modified] Pin
Jay0329-Jun-06 8:41
Jay0329-Jun-06 8:41 
AnswerRe: Static Variables Pin
toxcct29-Jun-06 8:58
toxcct29-Jun-06 8:58 
AnswerRe: Static Variables Pin
Jun Du29-Jun-06 8:58
Jun Du29-Jun-06 8:58 
AnswerRe: Static Variables Pin
David Crow29-Jun-06 8:58
David Crow29-Jun-06 8:58 
QuestionPrinting format of numbers Pin
mikobi29-Jun-06 7:41
mikobi29-Jun-06 7:41 
AnswerRe: Printing format of numbers Pin
David Crow29-Jun-06 8:10
David Crow29-Jun-06 8:10 
AnswerRe: Printing format of numbers Pin
Eric Dahlvang29-Jun-06 10:33
Eric Dahlvang29-Jun-06 10:33 
Questioninstallatio package creation Pin
Desmo1629-Jun-06 7:02
Desmo1629-Jun-06 7:02 
JokeRe: installatio package creation Pin
James R. Twine29-Jun-06 8:42
James R. Twine29-Jun-06 8:42 
Questionsuggestion..pls Pin
RockyJames29-Jun-06 7:01
RockyJames29-Jun-06 7:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.