Thursday, 18 August 2011

File transfer in c program

if(listen(sd,1)!=0)
    printf("Not listened\n");
else
    printf("Listened\n");




while(1)
 { 
    cd=accept(sd,NULL,NULL);

    printf("accept value %d\n",cd);

    recv(cd,buf,sizeof(buf),0);

    int fd=open(buf,O_RDONLY);

    read(fd,buf,1000);

    send(cd,buf,sizeof(buf),0);

    printf("Required File Information:%s\n",buf);

            //execute(buf);
close(cd);

}

if(connect(sockfd,(struct sockaddr *)&servaddr, sizeof(servaddr) )!=0)
{
    printf("client not connected to server\n");
    exit(0);
}
else
{
    printf("client connected to server\n");
}


   printf("\n Enter Required file name:");
scanf("%s",fname);

while(1)
 {

    send(sockfd,fname,sizeof(fname),0);

    recv(sockfd,fname,sizeof(fname),0);
 /*FILE *fp;
fp=fopen("ex3.c","w");
fputs(fname,fp);
 */
printf("SERVER Sending file datas%s\n",fname);

 }


//close client side connection
close(sockfd);







 writing strings into file using c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 50

int main()
{
  FILE *pFile = NULL;
  char *filename = "C:\\myfile.txt";
  char buffer[80] = "asdf";
  int buffer_size = BUFFER_SIZE;

  size_t str_length = 0;

  pFile = fopen(filename, "w");
  if(pFile == NULL)
  {
    printf("Error opening %s for writing. Program terminated.", filename);
    abort();
  }

  str_length = strlen(buffer);
  fwrite(&str_length, sizeof(size_t), 1, pFile);
  fwrite(buffer, str_length, 1, pFile);

  fclose(pFile);
  printf("\nFile write complete\n");
  if(buffer != NULL)
    free(buffer);

}







#include <stdio.h>

int main ()
{
FILE *pFile;
char str[10];
char buffer[10];
gets(str);
strcpy(buffer,str);
pFile = fopen ("myfile.txt" , "w");
fwrite (buffer,1,strlen(buffer),pFile);
fclose (pFile);
return 0;
}




#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
  char str[80];
  FILE *fp;

  if((fp = fopen("TEST", "w"))==NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }

  do {
    printf("Enter a string (CR to quit):\n");
    gets(str);
    strcat(str, "\n");  /* add a newline */
    fputs(str, fp);
  } while(*str!='\n');

  return 0;
}

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

int main(){

    char input[30];
    char* args[10];
    char argIndex = 0;
   
    printf("Enter command or program name: ");
    fgets(input,30,stdin);
   
    char* str;
    str = strtok(input, " ");
              
    while(str != NULL){
        args[argIndex] = str;
        argIndex++;        
        str = strtok(NULL, " ");
   }
  
   args[argIndex] = (char *) 0;

   execvp(args[0], args);

}




str = strtok(NULL, " \n");

No comments:

Post a Comment