• 0

Reading a dropbox text file help!


Question

Hi

I am currently making a program for a local radio station.

Their schedule updates regularly, but as it is quite hard to push out an update for the whole program each time this happens, i am hoping to store the schedule in a text file on dropbox, and to have it display in a label, in the program.

 

I cant figure out how to make the label say the contents of the text file, and I was wondering if someone would help me to get over my issue.

 

Thanks in Advance

Link to comment
https://www.neowin.net/forum/topic/1263452-reading-a-dropbox-text-file-help/
Share on other sites

2 answers to this question

Recommended Posts

  • 0
  On 08/07/2015 at 19:59, Harry Smith said:

Hi

I am currently making a program for a local radio station.

Their schedule updates regularly, but as it is quite hard to push out an update for the whole program each time this happens, i am hoping to store the schedule in a text file on dropbox, and to have it display in a label, in the program.

I cant figure out how to make the label say the contents of the text file, and I was wondering if someone would help me to get over my issue.

Thanks in Advance

The actual code will differ depending on the language, but here's a general outline:

a) Open & read target file into program buffer. The type of program buffer will largely depend on the data format of the file you're reading and what language you're using. For simplicity's sake, let's say you're reading plain text:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int
main ( int argc, char **argv ) {

	int		fd;
	struct stat 	file;
	char		*text;
	size_t		result;

	if ( argc < 2 ) {
		fprintf ( stderr, "Please provide a schedule file argument!\n" );
		return EXIT_FAILURE;
	}

	if ( -1 == ( fd = open ( argv[1], O_RDONLY ) ) ) {
		fprintf ( stderr, "Failed to open schedule: %s\n", strerror ( errno ) );
		return EXIT_FAILURE;
	}

	fstat ( fd, &file );  
	text = malloc ( file.st_size + 1 );

	result = read ( fd, text, file.st_size );
	
	/* zero terminate it */
	text[file.st_size] = '\0';
	
	printf ( "Successfully read schedule (%u bytes) into program buffer.\n", result );

	/* do something with the text.. */

	/* clean up */
	close ( fd );
	free ( text );

	return EXIT_SUCCESS;
}	
$ gcc -o schedule schedule.c && ./schedule file.txt
Successfully read schedule (858 bytes) into program buffer.
b) Display plain text in a GUI. Again this is highly dependent on the toolkit used. Most of them have widgets that can display text. For instance, in GTK:

gtk_label_new ( text );
Or:

GtkLabel *schedule = gtk_label_new ( NULL );
gtk_label_set_text ( schedule, text );
Your toolkit should provide something similar.
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.