因此,我知道效率取决于解决方案中使用的算法和数据结构.但是,我真的不知道算法的顺序如何比处理器的速度更重要?
该程序应该将文件的内容从客户端发送到服务器端的输出文件。但是,我的代码适用于少数文件,不适用于大多数文件。例如,如果我尝试将调用的文件的内容复制morefood.txt到输出文件 say picolo.txt,则不会复制任何内容。
服务器代码:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int main(int argc, char *argv[]){
int fd =0, confd = 0;
struct sockaddr_in serv_addr;
char buff[1025];
int num;
fd = socket(AF_INET, SOCK_STREAM, 0);
printf("Socket created\n");
memset(&serv_addr, '0', sizeof(serv_addr));
memset(buff, '0', sizeof(buff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(fd, 10);
FILE* fp = fopen( "picolo.txt", "wb");
if(fp == NULL){ …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个可以搜索文件(称为student.txt)中的字符串的程序。我希望我的程序在文件中找到相同的单词时打印该单词,但它显示错误。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int num =0;
char word[2000];
char *string[50];
FILE *in_file = fopen("student.txt", "r");
//FILE *out_file = fopen("output.txt", "w");
if (in_file == NULL)
{
printf("Error file missing\n");
exit(-1);
}
while(student[0]!= '0')
{
printf("please enter a word(enter 0 to end)\n");
scanf("%s", student);
while(!feof(in_file))
{
fscanf(in_file,"%s", string);
if(!strcmp(string, student))==0//if match found
num++;
}
printf("we found the word %s in the file %d times\n",word,num );
num = 0;
}
return 0; …Run Code Online (Sandbox Code Playgroud)