"no newline at end of file"丙
"no newline at end of file" C
我正在使用此命令进行编译:
gcc –Werror –std=c99 client.c –o client
代码:
#include <string.h>
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUFFER_SIZE 1000
#define PORT_NUM 8888
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[BUFFER_SIZE];
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,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
while (1)
{
bzero(buffer, BUFFER_SIZE);
fgets(buffer, BUFFER_SIZE, stdin);
if((strncmp(buffer,"close",5) == 0 ))
break;
printf("Please enter the message: ");
bzero(buffer, BUFFER_SIZE);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer, BUFFER_SIZE);
n = read(sockfd,buffer, BUFFER_SIZE-1);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
}
close(sockfd);
return 0;
}
正在尝试在OS linux 的VM 上的shell 中编译它
这个错误让我发疯,我不知道该怎么做才能解决这个问题。
我以为 client.c 文件损坏了,所以我复制并粘贴到一个新的 .c 文件中。当失败时,我从 0 开始写,但仍然出现此错误。
提前致谢!
在右括号后面添加 Enter(新行)。
文本处理程序并不总是同意 \n
字符是 行分隔符 还是 行终止符 -区别在于文件中的最后一行是否应后跟 \n
字符。
我从未见过 GCC 抱怨过这个,但你没有指出错误的实际来源。
您只需在文件的最后一行后按 ENTER 键即可纠正这种情况。
或者,您可以打开文件,然后他们使用 Vim 等编辑器保存文件,该编辑器将其视为一行 终止符.
作为比较,大多数 UNIX 工具也将 \n
视为行终止符,并将其包含在最后一行之后。这允许 cat
一个文件而不会破坏后面的任何内容(例如另一个文件、shell 提示等)
我正在使用此命令进行编译:
gcc –Werror –std=c99 client.c –o client
代码:
#include <string.h>
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUFFER_SIZE 1000
#define PORT_NUM 8888
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[BUFFER_SIZE];
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,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
while (1)
{
bzero(buffer, BUFFER_SIZE);
fgets(buffer, BUFFER_SIZE, stdin);
if((strncmp(buffer,"close",5) == 0 ))
break;
printf("Please enter the message: ");
bzero(buffer, BUFFER_SIZE);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer, BUFFER_SIZE);
n = read(sockfd,buffer, BUFFER_SIZE-1);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
}
close(sockfd);
return 0;
}
正在尝试在OS linux 的VM 上的shell 中编译它 这个错误让我发疯,我不知道该怎么做才能解决这个问题。 我以为 client.c 文件损坏了,所以我复制并粘贴到一个新的 .c 文件中。当失败时,我从 0 开始写,但仍然出现此错误。
提前致谢!
在右括号后面添加 Enter(新行)。
文本处理程序并不总是同意 \n
字符是 行分隔符 还是 行终止符 -区别在于文件中的最后一行是否应后跟 \n
字符。
我从未见过 GCC 抱怨过这个,但你没有指出错误的实际来源。
您只需在文件的最后一行后按 ENTER 键即可纠正这种情况。
或者,您可以打开文件,然后他们使用 Vim 等编辑器保存文件,该编辑器将其视为一行 终止符.
作为比较,大多数 UNIX 工具也将 \n
视为行终止符,并将其包含在最后一行之后。这允许 cat
一个文件而不会破坏后面的任何内容(例如另一个文件、shell 提示等)