Hi guys, am currently creating a client/server program. However im having trouble reading from a text file. Im not sure whether fgets or fread is better in my case. Basically my program requires user to input a country, and the program will display its details out.My question is, what method should i use to store the txt file into a tempdatabase?(or simply use the txt file) and secondly how do I display the required fields only. Is strtoken() the only way?
For example.
Please input country: Czech Republic
Czech Republic capital: Prague
Czech Republic Currency Code: CZK
this is the code im written, however its only the server/client portion as i have no idea how to read the particular file(Countries.txt). Any help is appreciated. Currently running on ubuntu 9.1 on vmware.
server.c
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h> /* for sockaddr_un struct */
#define DEFAULT_PROTOCOL 0
/* POSIX renames "Unix domain" as "local IPC."
Not all systems define AF_LOCAL and PF_LOCAL (yet). */
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif
/****************************************************************/
main ()
{
int serverFd, clientFd, serverLen, clientLen;
struct sockaddr_un serverAddress;/* Server address */
struct sockaddr_un clientAddress; /* Client address */
struct sockaddr* serverSockAddrPtr; /* Ptr to server address */
struct sockaddr* clientSockAddrPtr; /* Ptr to client address */
/* Ignore death-of-child signals to prevent zombies */
signal (SIGCHLD, SIG_IGN);
serverSockAddrPtr = (struct sockaddr*) &serverAddress;
serverLen = sizeof (serverAddress);
clientSockAddrPtr = (struct sockaddr*) &clientAddress;
clientLen = sizeof (clientAddress);
/* Create a socket, bidirectional, default protocol */
serverFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
serverAddress.sun_family = AF_LOCAL; /* Set domain type */
strcpy (serverAddress.sun_path, "Countries"); /* Set name */
unlink ("Countries"); /* Remove file if it already exists */
bind (serverFd, serverSockAddrPtr, serverLen); /* Create file */
listen (serverFd, 5); /* Maximum pending connection length */
while (1) /* Loop forever */
{
/* Accept a client connection */
clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);
if (fork () == 0) /* Create child to send countries */
{
writeCountries (clientFd); /* Send the countries */
close (clientFd); /* Close the socket */
exit (/* EXIT_SUCCESS */ 0); /* Terminate */
}
else
close (clientFd); /* Close the client descriptor */
}
}
/****************************************************************/
client.c
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h> /* for sockaddr_un struct*/
#define DEFAULT_PROTOCOL 0
/* POSIX renames "Unix domain" as "local IPC."
Not all systems define AF_LOCAL and PF_LOCAL (yet). */
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif
/****************************************************************/
main ()
{
int clientFd, serverLen, result;
struct sockaddr_un serverAddress;
struct sockaddr* serverSockAddrPtr;
serverSockAddrPtr = (struct sockaddr*) &serverAddress;
serverLen = sizeof (serverAddress);
/* Create a socket, bidirectional, default protocol */
clientFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
serverAddress.sun_family = AF_LOCAL; /* Server domain */
strcpy (serverAddress.sun_path, "country"); /* Server name */
do /* Loop until a connection is made with the server */
{
result = connect (clientFd, serverSockAddrPtr, serverLen);
if (result == -1) sleep (1); /* Wait and then try again */
}
while (result == -1);
readCountry (clientFd); /* Read the country */
close (clientFd); /* Close the socket */
exit (/* EXIT_SUCCESS */ 0); /* Done */
}
/**************************************************************/
readCountries (fd)
int fd;
{
char str[200];
while (readLine (fd, str)) /* Read lines until end-of-input */
printf ("%s\n", str); /* Echo line from socket */
}
/**************************************************************/
Countries.txt
CZ,[b]Czech Republic[/b],EZ,CZ,CZE,203.00,[b]Prague[/b],Europe,[b]Czech Koruna,CZK,[/b]10264212.00
DE,Germany,GM,DE,DEU,276.00,Berlin,Europe,Euro,EUR,83029536.00
DJ,Djibouti,DJ,DJ,DJI,262.00,Djibouti,Africa,Djibouti Franc,DJF,460700.00
DK,Denmark,DA,DK,DNK,208.00,Copenhagen,Europe,Danish Krone,DKK,5352815.00
DM,Dominica,DO,DM,DMA,212.00,Roseau,Central America and the Caribbean,East Caribbean Dollar,XCD,70786.00
DO,Dominican Republic,DR,DO,DOM,214.00,Santo Domingo,Central America and the Caribbean,Dominican Peso,DOP,8581477.00
DZ,Algeria,AG,DZ,DZA,12.00,Algiers,Africa,Algerian Dinar,DZD,31736053.00
EC,Ecuador,EC,EC,ECU,218.00,Quito,South America,US dollar,USD,13183978.00
EE,Estonia,EN,EE,EST,233.00,Tallinn,Europe,Kroon,EEK,1423316.00
The important fields are: Field 2,7,9,10 , which is the fields i want to extract out when user inputs the correct country.