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; 
}

What exactly is the problem. Sample Output?

line 73: when i check the contents of the file temp2.txt in each loop, there is no problem. as in, the value changes in each run.
line 78: i print the value of line2 after reading the line from temp2.txt into line2. here, the value of line2 printed is always the value corresponding to the first run of the loop. it does not seem to change with the changing value of temp2.txt 's contents.

system("cksum temp.txt > temp2.txt"); /* You updated this from a System Call
                                         There can be a possibility that since the                                          
                                         file is locked by your program, the file 
                                         isn't updated
                                         Or if the call was successful the Shell                                          
                                         updated the file's contents without 
                                         informing your application (PostMessage)
                                         */
printf("########");
//system("cat temp2.txt");

rewind(temp2);                           /*Instead of rewind, use:
                                         fclose(temp2);
                                         temp2=fopen("temp2.txt","w+");
                                         */
                                         /* However you are never writing to temp2.txt
                                         in your program so just open it in "r" mode
                                         */
                                         

if(temp2)
{
fgets(line2, sizeof line2, temp2);
printf("%s\n", line2);
}

This ought to be sufficient.

What is the objective of your program ? Are you supposed to read from a file, do some processing on the string and send the string across ? If yes then I think you are over complicating a simple program ?

@abhimanipal
yes.. thats what i'm trying to do. i do feel i'm over complicating the program. but i am trying to use the built in cksum for the computations and i couldn't figure out an other way.
perhaps the wiser thing would be to write a code within the program to compute the checksum. or use pipe and dup2 to get the output from cksum?
@nbaztec
thank you!

Does nbaztec suggestion solve your problem ?
Also instead of using text files, may be that could solve your problem

@xenanovich You're welcome. If problem is fixed kindly mark the thread as Solved.
@abhi

Also instead of using text files, may be that could solve your problem

Which method are your referring to?

hi,
i used fputs instead of fprintf in line 69. fprintf wasn't writing to the file for some reason.
and instead of using so many temp files i used popen() to get the output of cksum into a string. works fine now. thanks.

fprintf(temp,&newarra[i]);

printf wasn't writing to the file for some reason.

Yes, there in fact WAS a reason. The syntax of fprintf() was incorrect.

fprintf(<stream>,<format>,<args>);
fprintf(temp,"%s",newarra[i]);

Your earlier declaration didn't show error as fprintf() used newarra as a (char *) which is the type of <format string> but didn't use any arguments.

Also newarra is itself a pointer to first element you don't really need to take it's address.

thanks nisheeth!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.