Hi,
i am trying to read the contents of a text file containing a single line into a string.
i'm doing this in a while loop.
the text file gets updated in each run of the loop and therefore the string should contain a new value in each run.
but the string is not getting updated. the file is getting updated but the value in the string remains that of the first run.
here is the relevant code:
sending side:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n,j;
int i=0;
//int try[12];
struct sockaddr_in serv_addr;
struct hostent *server;
char line[1024], line2[256];
static const char filename[] = "serialfile2.txt";
static const char filedest[] = "serialdest.txt";
FILE *file = fopen ( filename, "r" );
FILE *filedest2 = fopen( filedest, "w");
FILE *temp= fopen("temp.txt","w");
FILE *temp2=fopen("temp2.txt","w+");
char arra[1024][1024];
char newarra[256][256];
for(i=0; i<1024; i++)
line[i] = '\0';
for(i=0; i<256; i++)
line2[i] = '\0';
for(i=0; i<1024; i++)
for(j=0; j<1024; j++)
arra[i][j] = '\0';
for(i=0; i<256; i++)
for(j=0; j<256; j++)
newarra[i][j] = '\0';
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
while ( fgets ( line, sizeof line, file ) != NULL ){
strcpy(arra[i], line);
strncpy(newarra[i], arra[i]+16, 30);
rewind(temp);
fprintf(temp,&newarra[i]);
//system("cat temp.txt");
system("cksum temp.txt > temp2.txt");
printf("########");
//system("cat temp2.txt");
rewind(temp2);
if(temp2)
{
fgets(line2, sizeof line2, temp2);
printf("%s\n", line2);
}
strncpy(newarra[i]+30,line2,12);
n = write(sockfd,newarra[i],strlen(newarra[i]));
n = read(sockfd, newarra[i],strlen(newarra[i]));
printf("%s\n",&newarra[i]);
fprintf(filedest2, &newarra[i]);
fprintf(filedest2, "\n");
i++;
}
fclose(temp);
fclose(temp2);
return 0;
}
receiving side:
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
//sockfd returned by socket()
//newsockfd returned by accept()
//portno=port on which server is accepting connections
int count=1;
char buffer[256];
//read freom this buffer
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
//set serv_addr to zero
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
//file operations come here.
bzero(buffer,256);
// n = read(newsockfd,buffer,255);
while((read(newsockfd,buffer,255))>0){
fflush(stdout);
fflush(stdin);
// if (n < 0) error("ERROR reading from socket");
//printf("****************PACKET NUMBER: %d *******************************\n",count);
count++;
printf("\n %s\n",buffer);
n = write(newsockfd,"sending...",18);
if (n < 0) error("ERROR writing to socket");
}
return 0;
}