Exemplo n.º 1
0
int main()
{
	list *head=readfromfile();
	rebuild(head);//排序重建
	int num;
	printf("演示————\n\n输入查找的列:");
	fflush(stdin);
	scanf("%d",&num);
	printcol(head,num);

	printf("\n\n输入查找的行:");
	fflush(stdin);
	scanf("%d",&num);
	printrow(head,num);
	int x,y;
	printf("\n\n输入查找的坐标(x y):");
	fflush(stdin);
	scanf("%d%d",&x,&y);
	printone(head,x,y);
	char name[20]="";
	printf("\n\n输入查找的人名:");
	fflush(stdin);
	scanf("%s",name);
	printaperson(head,name);
	//查找就那么遍历链表就行了,没写。
	
	printsitchart(head);
	return 0;
}
Exemplo n.º 2
0
int main()
{
    List* L;
    List* Hash[101];
    for(int i=0; i<101; i++)
    {
        Hash[i]=(List*) calloc (1, sizeof(List));
        list_ctor(Hash[i]);
    }
    L=(List*) calloc (1, sizeof(List));
    list_ctor(L);
    FILE* F;
    F=fopen("1.txt","r");
    assert(F);
    while(!feof(F))
    {
        readfromfile(F,L);
    }



    hasher(L,Hash,4);



    for (int i=0; i<101; i++)printf("i=%5d  %d\n",i,Hash[i]->amount);


}
Exemplo n.º 3
0
int setReadPos(TWKB_BUF *tb,long int read_pos, size_t len)
{

	if(the_file)
	{

		
		if(read_pos>=tb->BufOffsetFromBof && (read_pos + len) <= (tb->BufOffsetFromBof + (tb->end_pos - tb->start_pos)))
		{
			/*Ok, we already have the buffer we need
			Just place the cursor and return*/
			tb->read_pos = tb->start_pos + (read_pos-tb->BufOffsetFromBof);
			return 0;
		}
		else
		{		
			if(fseek(the_file, read_pos, SEEK_SET))
				return 1;
		
			return readfromfile(tb,the_file, len);
		}
	}
	
	return 1;
	
}
Exemplo n.º 4
0
int
jumpandread(TWKB_BUF *tb,size_t jump, size_t len )
{
	size_t unread_in_buffer;
	if(the_file)
	{
		//Do we have what we need in the buffer already?
		unread_in_buffer = tb->end_pos - tb->read_pos;
		
		if(unread_in_buffer>=jump + len)
		{
			/*Ok, we already have the buffer we need
			Just place the cursor and return*/
			tb->read_pos = tb->read_pos + jump;
			return 0;
		}
		else
		{		
			if(fseek(the_file, jump - unread_in_buffer, SEEK_CUR))
			{
				
				printf("problems to jump %d\n",(int) (jump - unread_in_buffer));
				return 1;
			}
		
			return readfromfile(tb,the_file, len);
		}
	}
	
	printf("%s\n","Couldn't find file");
	return 1;
}
Exemplo n.º 5
0
int
readmore(TWKB_BUF *tb )
{
	if(the_file)
		return readfromfile(tb,the_file,READ_CHUNKS_SIZE);
	else
		return 1;
}
Exemplo n.º 6
0
/*
 * Curl_FormReader() is the fread() emulation function that will be used to
 * deliver the formdata to the transfer loop and then sent away to the peer.
 */
size_t Curl_FormReader(char *buffer,
                       size_t size,
                       size_t nitems,
                       FILE *mydata)
{
  struct Form *form;
  size_t wantedsize;
  size_t gotsize = 0;

  form=(struct Form *)mydata;

  wantedsize = size * nitems;

  if(!form->data)
    return 0; /* nothing, error, empty */

  if((form->data->type == FORM_FILE) ||
     (form->data->type == FORM_CALLBACK)) {
    gotsize = readfromfile(form, buffer, wantedsize);

    if(gotsize)
      /* If positive or -1, return. If zero, continue! */
      return gotsize;
  }
  do {

    if((form->data->length - form->sent ) > wantedsize - gotsize) {

      memcpy(buffer + gotsize , form->data->line + form->sent,
             wantedsize - gotsize);

      form->sent += wantedsize-gotsize;

      return wantedsize;
    }

    memcpy(buffer+gotsize,
           form->data->line + form->sent,
           (form->data->length - form->sent) );
    gotsize += form->data->length - form->sent;

    form->sent = 0;

    form->data = form->data->next; /* advance */

  } while(form->data && (form->data->type < FORM_CALLBACK));
  /* If we got an empty line and we have more data, we proceed to the next
     line immediately to avoid returning zero before we've reached the end. */

  return gotsize;
}
Exemplo n.º 7
0
/*
 * Curl_FormReader() is the fread() emulation function that will be used to
 * deliver the formdata to the transfer loop and then sent away to the peer.
 */
size_t Curl_FormReader(char *buffer,
                       size_t size,
                       size_t nitems,
                       FILE *mydata)
{
  struct Form *form;
  size_t wantedsize;
  size_t gotsize = 0;

  form=(struct Form *)mydata;

  wantedsize = size * nitems;

  if(!form->data)
    return 0; /* nothing, error, empty */

  if(form->data->type == FORM_FILE)
    return readfromfile(form, buffer, wantedsize);

  do {

    if( (form->data->length - form->sent ) > wantedsize - gotsize) {

      memcpy(buffer + gotsize , form->data->line + form->sent,
             wantedsize - gotsize);

      form->sent += wantedsize-gotsize;

      return wantedsize;
    }

    memcpy(buffer+gotsize,
           form->data->line + form->sent,
           (form->data->length - form->sent) );
    gotsize += form->data->length - form->sent;

    form->sent = 0;

    form->data = form->data->next; /* advance */

  } while(form->data && (form->data->type == FORM_DATA));
  /* If we got an empty line and we have more data, we proceed to the next
     line immediately to avoid returning zero before we've reached the end.
     This is the bug reported November 22 1999 on curl 6.3. (Daniel) */

  return gotsize;
}
Exemplo n.º 8
0
int
buffer_from_file(char *file_name,TWKB_BUF *tb)
{
	tb->start_pos = tb->read_pos = malloc(READ_CHUNKS_SIZE);
	tb->end_pos=tb->max_end_pos = tb->start_pos + READ_CHUNKS_SIZE;
	the_file = fopen(file_name, "rb");
	if (the_file)
	{		
		tb->handled_buffer = 1;
		readfromfile(tb,the_file,READ_CHUNKS_SIZE);
		return 0;		
	}
	
	char *errmsg = strerror(errno);
	fprintf(stderr,"Error: %s: \"%s\"\n",errmsg, file_name);
	exit(EXIT_FAILURE);
}
Exemplo n.º 9
0
/*
 * curl_formget()
 * Serialize a curl_httppost struct.
 * Returns 0 on success.
 *
 * @unittest: 1308
 */
int curl_formget(struct curl_httppost *form, void *arg,
                 curl_formget_callback append)
{
  CURLcode result;
  curl_off_t size;
  struct FormData *data, *ptr;

  result = Curl_getformdata(NULL, &data, form, NULL, &size);
  if(result)
    return (int)result;

  for(ptr = data; ptr; ptr = ptr->next) {
    if((ptr->type == FORM_FILE) || (ptr->type == FORM_CALLBACK)) {
      char buffer[8192];
      size_t nread;
      struct Form temp;

      Curl_FormInit(&temp, ptr);

      do {
        nread = readfromfile(&temp, buffer, sizeof(buffer));
        if((nread == (size_t) -1) ||
           (nread > sizeof(buffer)) ||
           (nread != append(arg, buffer, nread))) {
          if(temp.fp)
            fclose(temp.fp);
          Curl_formclean(&data);
          return -1;
        }
      } while(nread);
    }
    else {
      if(ptr->length != append(arg, ptr->line, ptr->length)) {
        Curl_formclean(&data);
        return -1;
      }
    }
  }
  Curl_formclean(&data);
  return 0;
}
Exemplo n.º 10
0
/*
 * curl_formget()
 * Serialize a curl_httppost struct.
 * Returns 0 on success.
 */
int curl_formget(struct curl_httppost *form, void *arg,
                 curl_formget_callback append)
{
  CURLcode rc;
  curl_off_t size;
  struct FormData *data, *ptr;

  rc = Curl_getFormData(&data, form, NULL, &size);
  if (rc != CURLE_OK)
    return (int)rc;

  for (ptr = data; ptr; ptr = ptr->next) {
    if (ptr->type == FORM_FILE) {
      char buffer[8192];
      size_t read;
      struct Form temp;

      Curl_FormInit(&temp, ptr);

      do {
        read = readfromfile(&temp, buffer, sizeof(buffer));
        if ((read == (size_t) -1) || (read != append(arg, buffer, read))) {
          if (temp.fp) {
            fclose(temp.fp);
          }
          Curl_formclean(&data);
          return -1;
        }
      } while (read == sizeof(buffer));
    } else {
      if (ptr->length != append(arg, ptr->line, ptr->length)) {
        Curl_formclean(&data);
        return -1;
      }
    }
  }
  Curl_formclean(&data);
  return 0;
}
Exemplo n.º 11
0
int main()
{
    struct sockaddr_in si_me, si_other;
    int s, i=1, slen=sizeof(si_other),c_index,count,cmds;
    char buf[BUFLEN];

    /* Create a pipe */
    pipe(sockets);

    /* Create a socket */
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
        diep("socket");

    memset((char *) &si_me, 0, sizeof(si_me));
    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);

    /* Socket s should be bound to the address in si_me */
    if (bind(s, (struct sockaddr *)&si_me, sizeof(si_me))==-1)
        diep("bind");

    //deep init_device();

    /* Create a thread */
    pthread_t threads[NTHREADS];
    int thread_args[NTHREADS];
    thread_args[i] = i;
    pthread_create(&threads[i], NULL, thread1/*mainloop*/, (void *) &thread_args[i]);

    while(1)
    {
        printf("Waiting for USER...\n");

        /* Receive a packet from s, that the data should be put into buf */
        if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1)
            diep("recvfrom()");

        /* Receive a packet from s, that the data should be put into itr */
        if (recvfrom(s, itr, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1)
            diep("recvfrom()");
        printf("Option: %s Capture: %s\n",buf,itr);

        /* Convert a string to an integer */
        cmds = atoi(buf);
        itrs = atoi(itr);

        switch (cmds)
        {
        case 1:
            printf("Validation\n");
            capture_event=1;
            for(count=0; count<itrs; count++)
            {

                /* It reads the data from IPC socket */
                read(sockets[0], t_buff, 200);
                c_index = atoi(t_buff);
                //deep process_image (buffers[c_index][0].start);
                sprintf(s_buf, "%d\n", buffers[c_index][0]);
                printf("Sending data %s\n\n",s_buf);
                /* Send BUFLEN bytes from s_buf to s */
                if (sendto(s, s_buf, BUFLEN, 0, (struct sockaddr *)&si_other, slen)==-1)
                    diep("sendto()");
            }
            readfromfile();
            capture_event=0;
            break;

        case 2:
            printf("Registeration\n");
            capture_event=1;
            for(count=0; count<itrs; count++)
            {

                /* It reads the data from IPC socket */
                read(sockets[0], t_buff, 200);
                c_index = atoi(t_buff);
                //deep process_image (buffers[c_index][0].start);
                sprintf(s_buf, "%d\n", buffers[c_index][0]);
                printf("Sending data %s\n\n",s_buf);

                /* Send BUFLEN bytes from s_buf to s */
                if (sendto(s, s_buf, BUFLEN, 0, (struct sockaddr *)&si_other, slen)==-1)
                    diep("sendto()");
            }
            writetofile();
            capture_event=0;
            break;
        }
    }
    close(s);
    return 0;
}
Exemplo n.º 12
0
int main()
{
    struct idhash allstu;
    struct student *p;
    struct student tempstu;
    struct student *stu;
    int tempage;
    char cmd[1024];
    int cmdnum;
    int quit = 0;
    int select;
    FILE *studata;

    studata = fopen("studata","r+");
    if(NULL == studata)
    {
        studata = fopen("studata","w+");
        if(NULL == studata)
        {
            printf("cannot open file !\n");
            return -1;
        }
    }
    
    initallidhash(&allstu);
    readfromfile(&allstu,studata);
    printf("|-------------------------------------------|\n");
    printf("|----Welcome to Student Database System-----|\n");
    printf("|-------------------------------------------|\n\n");
    printf("%d student have!\n",count(&allstu));

    while(0 == quit)
    {
        printf("|-------------------------------------------|\n");
        printf("|-------Please Input \"help\" for Help--------|\n");
        printf("|-------------------------------------------|\n");
        
        fgets(cmd,1024,stdin);
        cmd[strlen(cmd)-1] = 0;
        cmdnum = getcmd(cmd);
        switch(cmdnum)
        {
            case CMDSHOW:
                showallstu(&allstu);
                break;
            case CMDQUIT:
                printf("quit successful!\n");
                quit = 1;
                break;
            case CMDADD:
                printf("please input a new student:\nstudent's ID:");
                scanf("%s",tempstu.id);
                printf("please input student's Name:");
                scanf("%s",tempstu.name);
                printf("please input student's age:");
                scanf("%d",&tempage);
                tempstu.age = (char)tempage;
                fgets(cmd,1024,stdin);
                printf("please input student's sex:");
                scanf("%c",&tempstu.sex);
                fgets(cmd,1024,stdin);
                tempstu.next = NULL;
                p = findstu(&allstu,tempstu.id);
                if(NULL != p)
                {
                    printf("This id is already exist!\n");
                    break;
                }
                stu = newstu(&tempstu);
                addstu(&allstu,stu);
                break;
            case CMDDEL:
                printf("please input the id you wante to delete:");
                scanf("%s",tempstu.id);
                fgets(cmd,1024,stdin);
                p = findstu(&allstu,tempstu.id);
                if(NULL == p)
                {
                    printf("not found the id!\n");
                    break;
                }
                delstu(&allstu,p);
                break;
            case CMDFIND:
                printf("please input 1 for id,2 for name:");
                scanf("%d",&select);
                fgets(cmd,1024,stdin);
                if(1 == select)
                {
                    scanf("%s",tempstu.id);
                    fgets(cmd,1024,stdin);
                    p = findstu(&allstu,tempstu.id);
                    if(NULL == p)
                    {
                        printf("not found the id!\n");
                        break;
                    }
                    showstu(p);
                }
                /*
                else if(2 == select)
                {
                    printf("please input the name you want to find:");
                    scanf("%s",tempstu.name);
                    fgets(cmd,1024,stdin);
                    p = phead;
                    do
                    {
                        p = findnamenext(p,tempstu.name);
                        if(NULL != p)
                        {
                            showstu(p);
                            p = p->next;
                        }
                    }while(NULL != p);
                    break;
                }
                */
            case CMDSAVE:
                save(&allstu,studata);
                break;
            case CMDREVERSE:
                //phead = reverse(phead);
                break;
            case CMDHELP:
                showhelp();
                break;
            default:
                printf("not this cmd\n");
                break;
        }
    }
    fclose(studata);
}
Exemplo n.º 13
0
Arquivo: main.c Projeto: damora/ivr
//-------------------------------------------------------------------------------------------------------------------//
//readvoldata
//-------------------------------------------------------------------------------------------------------------------//
int readvoldata(char * filename)
{
    long long localsize;

#ifndef MPI
    FILE *fd;

    // get volume
    if ((fd=fopen(filename, "r")) == NULL) {
        fprintf(stderr, "file open failed: %p\n", fd);
        return -1;
    }
    localsize = (long long) (volume.localdims.w * volume.localdims.h * volume.localdims.d);

    // allocate memory and read data
    volume.data.f = (float *) malloc(sizeof(float) * localsize);
    assert(volume.data.f != NULL);

    size_t num = fread( (float *) volume.data.f, localsize, sizeof(float), fd);
    assert(num > 0);
    fclose(fd);

#else // MPIIO distributed parallel volume rendering case
    int rank,  comsize;
    int localbytes;
    int periods[3] = {1, 1, 1};
    int startindex[3];
    int procdims[3] = {volume.procdims.y, volume.procdims.x, volume.procdims.z};
    //MPI_Status status;
    MPI_Comm comm;

    // get rank and comm size
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &comsize);
    fprintf(stderr,"rank:%d, comsize:%d\n", rank, comsize);

    // in MPIIO terms dims represent row, column, slab which maps to h, w, d
    int globaldims[3] = {volume.globaldims.h, volume.globaldims.w, volume.globaldims.d};
    int localdims[3] = {volume.localdims.h, volume.localdims.w, volume.localdims.d};

    // start part where we create a cartesian coordinate communicator
    int coords[3];
    MPI_Cart_create(MPI_COMM_WORLD, 3, procdims, periods, 0, &comm);
    MPI_Comm_rank(comm, &rank);
    MPI_Cart_coords(comm, rank, 3, coords);

    // WE WILL USE THE VOLUME BEGIN AND ORIGIN WE COMPUTED IN INITVOLDIMS
    // start of subarray definition
    startindex[0]  =  volume.startindex.y;
    startindex[1]  =  volume.startindex.x;
    startindex[2]  =  volume.startindex.z;

    localsize = localdims[0] * localdims[1] * localdims[2];
    localbytes = localsize * sizeof(float);
    fprintf(stderr,"rank: %d, localbytes=%d\n", rank, localbytes);
    volume.data.f = (float *) malloc(localbytes);
    assert(volume.data.f != NULL);

    readfromfile(filename, volume.data.f, globaldims, localdims, startindex);
    fprintf(stderr,"rank: %d, startindex[0], startindex[1], startindex[2]=%d, %d, %d\n", rank, startindex[0], startindex[1], startindex[2]);
    //MPI_Get_count(&status, MPI_FLOAT, &count);
    MPI_Comm_free(&comm);

#endif

    // if there is byteswapping we'll do it here
    if (volume.byteswap) {
        for (int i=0; i<localsize; i++) {
            float in,out;
            in = volume.data.f[i];

            out = BYTESWAP_FP(in);
            volume.data.f[i] = out;
        }
    }

    return 0;
}