Ok, thanks for the help!
-- Wed Mar 04, 2009 12:20 pm --
Hi all
Still absolutely stumped when it comes to creating the loop for my username and password files. I tried implementing some of the code given above but just got tons of errors. I'm pretty rubbish at C, but basically i've established a connection with the server and got it to open and read both the username and passwords files. Basically i'm wondering does anyone have any input on how to do the next stage, i.e. creating the loop for the username and password files or have any decent resources for this information?
Sorry if the code is messy!
- Code: Select all
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("connect() failed");
echoStringLen = strlen(echoString);
if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send() sent a differnet number of bytes then expected");
totalBytesRcvd = 0;
printf("Received: ");
while (totalBytesRcvd < echoStringLen)
{
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE -1, 0)) <=0 )
DieWithError("recv() failed or connection closed prematurely");
totalBytesRcvd += bytesRcvd;
echoBuffer[bytesRcvd] = '\0';
printf(echoBuffer);
}
printf("\n");
HandleTCPServer(sock, argv);
close(sock);
printf("Enter the name of the username file you want to read data from: ");
scanf("%s", userfile);
if (!(fp=fopen(userfile,"r"))) {
perror(userfile);
exit(1);
}
/* count the number of comment lines, and data lines */
while (fgets(templine,sizeof(templine),fp)!=NULL) {
if (templine[0]=='#') {
comment++;
} else {
nrows++;
}
}
printf("\nThere are %u comments and %u rows", comment, nrows);
/* allocate string array to write all the lines from the file */
buff=malloc((comment+nrows)*sizeof(*buff));
if (buff==NULL) {
printf("\nCould not allocate pointer array");
}
for (i=0; i<(comment+nrows); i++) {
buff[i]=malloc(linelen*sizeof(*buff[i]));
if (buff[i]==NULL) {
printf("\nCould not allocate array %u", i);
}
}
/* read all lines into buffer, and print to screen */
rewind(fp);
for (i=0; i<(comment+nrows); i++) {
fgets(buff[i],linelen*sizeof(buff[i]),fp);
printf("%s", buff[i]);
}
/* clean up memory */
fclose(fp);
for (i=0; i<(comment+nrows); i++) {
free(buff[i]);
}
free(buff);
/* open a file to read */
printf("Enter the name of the password file you want to read data from: ");
scanf("%s", passfile);
if (!(fp1=fopen(passfile,"r"))) {
perror(passfile);
exit(1);
}
/* count the number of comment lines, and data lines */
while (fgets(templine1,sizeof(templine1),fp1)!=NULL) {
if (templine1[0]=='#') {
comment1++;
} else {
nrows1++;
}
}
printf("\nThere are %u comments and %u rows", comment1, nrows1);
/* allocate string array to write all the lines from the file */
buff1=malloc((comment1+nrows1)*sizeof(*buff1));
if (buff1==NULL) {
printf("\nCould not allocate pointer array");
}
for (j=0; j<(comment1+nrows1); j++) {
buff1[j]=malloc(linelen1*sizeof(*buff1[j]));
if (buff1[j]==NULL) {
printf("\nCould not allocate array %u", j);
}
}
/* read all lines into buffer, and print to screen */
rewind(fp1);
for (j=0; j<(comment1+nrows1); j++) {
fgets(buff1[j],linelen1*sizeof(buff1[j]),fp1);
printf("%s", buff1[j]);
}
/* clean up memory */
fclose(fp1);
for (j=0; j<(comment1+nrows1); j++) {
free(buff1[j]);
}
free(buff1);
return 0;
exit(0);
}
Thanks