GString problem.



Hi

I'm having some problems in assigning some text to a GString.
I start with

GString *data;
data = g_string_new ("");

Then I read data from a file into a gchar *

fread (buffer, sizeof (gchar), 32, fp);
g_string_append (data, buffer);

The problem is that when I call g_string append, it append the the contents of
"buffer" but also some garbage is sneaking in. Whats the problem. I have pasted
the whole function in the end, just in case.

Archit Baweja

P.S. The Code.


static gint
file_load (PeacockFile *file)
{
	FILE *fp;
	gchar buffer[32];
	gint bytes_read;
	GString *data;

	/*
	 * Open file for reading and if a problem occurs, return an error code.
	 */
	fp = fopen (file->name, "r");
	g_return_val_if_fail (fp != NULL, -1);

	data = g_string_new ("");

	/*
	 * No problems found, go on load the file.
	 */
	while (1) {
		bytes_read = fread (buffer, sizeof (gchar), 32, fp);
		
		if (bytes_read > 0)
			data = g_string_append (data, buffer);

		if (bytes_read != 32 && (feof (fp) || ferror (fp)))
			break;
	}

	/*
	 * If any problems are encountered, return error code.
	 */
	g_return_val_if_fail (!ferror (fp), -1);

	/*
	 * Update the variables accordingly.
	 */
	file->data = data;

	return 0;
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]