本文整理汇总了C++中Fgets函数的典型用法代码示例。如果您正苦于以下问题:C++ Fgets函数的具体用法?C++ Fgets怎么用?C++ Fgets使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Fgets函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char *argv[])
{
if(signal(SIGTSTP,handler)==SIG_ERR||signal(SIGINT,handler) == SIG_ERR)
unix_error("signal error");
char cmdline[MAXLINE];
printf("%s\n","We have following commands in our main system: ");
printf("%s\n","help\t we are glad to offer you our best services...");
printf("%s\n","checkin\t welcome to join our Oxford Heights Community...");
printf("%s\n","checkout\t we are sorry to see you are leaving us...");
printf("%s\n","payrent\t thanks for being our loyal tenants...");
printf("%s\n","info\t get to know our super developer...");
printf("%s\n","IAmTheBoss\t to exit this system...");
printf("type 0 to return to main system:");
Fgets(cmdline, MAXLINE, stdin);
if(feof(stdin))
exit(0);
while( strcmp(cmdline, "0\n") != 0 ){
printf("type 0 to return to main system:");
Fgets(cmdline, MAXLINE, stdin);
if(feof(stdin))
exit(0);
}
exit(EXIT_SUCCESS);
}
示例2: main
int main(int argc,char ** argv)
{
int n;
FILE *fp;
char buff[MAXLINE],command[MAXLINE];
if(Fgets(buff,MAXLINE,stdin)==NULL)
err_quit("input error");
n=strlen(buff);
if(buff[n-1]=='\n')
buff[n-1]=0;
snprintf(command,sizeof(command),"cat %s",buff);
fp=Popen(command,"r");
while(Fgets(buff,MAXLINE,fp)!=NULL)
Fputs(buff,stdout);
Pclose(fp);
exit(0);
}
示例3: str_cli
void
str_cli (FILE *fp, int sockfd) {
int maxfdp1;
fd_set rset;
char sendline[MAXLINE], recvline[MAXLINE];
FD_ZERO(&rset);
for (;;) {
FD_SET (fileno(fp), &rset);
FD_SET (sockfd, &rset);
maxfdp1 = MAX (fileno(fp), sockfd) + 1;
Select (maxfdp1, &rset, NULL, NULL, NULL);
if (FD_ISSET (sockfd, &rset)) {
if (Readline (sockfd, recvline, MAXLINE) == 0) {
err_quit ("str_cli: server terminated prematurely");
}
Fputs (recvline, stdout);
}
if (FD_ISSET (fileno(fp), &rset)) {
if (Fgets (sendline, MAXLINE, fp) == NULL) {
return;
}
Writen (sockfd, sendline, strlen(sendline));
}
}
}
示例4: ftp_command
int ftp_command(char *buf,int success,FILE *out,char *fmt,...)
{
va_list va;
char line[1200];
int val;
va_start(va,fmt);
vsprintf(line,fmt,va);
va_end(va);
if (write(fileno(out),line,strlen(line)) < 0)
return(-1);
bzero(buf,200);
while(1) {
Fgets(line,sizeof(line),out);
#ifdef DEBUG
printf("%s",line);
#endif
if (*(line+3)!='-') break;
}
strncpy(buf,line,200);
val = atoi(line);
if (success != val) return(-1);
return(1);
}
示例5: readconfig
void readconfig()
{
// Allocate userpass char array
int i;
int j;
for(i=0; i<2; i++ ){
for(j=0; j<128; j++){
userpass[i][j] = malloc(40);
}
}
FILE *file;
char line[128];
int c = 0;
char *username;
char *password;
file = fopen("rrshusers.txt", "r");
if (file)
{
while (Fgets(line, sizeof(line), file) != NULL)
{
char *str = line;
username = strtok(str, " ");
password = strtok(NULL, " ");
strtok(password, "\n");
strcpy(userpass[0][c], username);
strcpy(userpass[1][c], password);
c++;
}
fclose(file);
}
}
示例6: dg_cli
/*
signal occur must in recvfrom ,or it must be a dead cycle
*/
void dg_cli(FILE *fp,int sockfd,const SA *pservaddr,socklen_t servlen){
int n;
const in on = 1;
char sendline[MAXLINE+1],recvline[MAXLINE+1];
socklen_t len;
struct sockaddr_in *preply_addr;
preply_addr = malloc(sizeof(struct sockaddr_in));
bzero(preply_addr);
SetSockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on));
Signal(SIGALAM,recvfrom_alarm);
len = servlen;
while(Fgets(sendline,MAXLINE,fp) != NULL){
sendto(sockfd,sendline,strlen(sendline),0,pservaddr,len);
alarm(5);
for(;;){
len=servlen;
n = recvfrom(sockfd,recvlien,MAXLINE,0,preply_addr,&len);
if(n < 0){
if(errno == EINTR)
break;
else
err_quit("recvfrom error!");
}else{
recvfrom[n] = 0;
printf("from %s : %s",inet_ntop(preply_addr->sin_family,preply_addr->sin_addr.s_addr,preply_addr->sin_len),recvfrom);
}
}
}
}
示例7: main
int main(int argc, char **argv)
{
int clientfd, port;
char *host, buf[MAXLINE];
rio_t rio;
if (argc != 3) {
fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
exit(0);
}
host = argv[1];
port = atoi(argv[2]);
clientfd = Open_clientfd(host, port);
Rio_readinitb(&rio, clientfd);
while (Fgets(buf, MAXLINE, stdin) != NULL) {
printf("client get: %s\n",buf);
Rio_writen(clientfd, buf, strlen(buf));
printf("client buf: %s\n",buf);
Rio_readlineb(&rio, buf, MAXLINE);
Fputs(buf, stdout);
}
// printf("outside\n");
Close(clientfd); //line:netp:echoclient:close
exit(0);
}
示例8: dg_cli
void
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
socklen_t len;
struct sockaddr *preply_addr;
preply_addr = Malloc(servlen);
while (Fgets(sendline, MAXLINE, fp) != NULL) {
Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
len = servlen;
n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
if (len != servlen || memcmp(pservaddr, preply_addr, len) != 0) {
printf("reply from %s (ignored)\n",
Sock_ntop(preply_addr, len));
continue;
}
recvline[n] = 0; /* null terminate */
Fputs(recvline, stdout);
}
}
示例9: client
void client(int readfd, int writefd)
{
size_t len;
ssize_t n;
char *ptr;
struct mymesg mesg;
//start buffer with pid and a blank
snprintf(mesg.mesg_data, MAXMESGDATA, "%ld", (long) getpid());
len = strlen(mesg.mesg_data);
ptr = mesg.mesg_data + len;
// read pathname
Fgets(ptr, MAXMESGDATA - len, stdin);
len = strlen(mesg.mesg_data);
if (mesg.mesg_data[len-1] == '\n')
len--;
mesg.mesg_len = len;
mesg.mesg_type = 1;
// write PID and pathname to IPC channed
mesg_send(writefd, &mesg);
// read from IPC, write to standard output
mesg.mesg_type = getpid();
while ((n = mesg_recv(readfd, &mesg)) > 0)
Write(STDOUT_FILENO, mesg.mesg_data, n);
}
示例10: dg_cli
void
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
Signal(SIGALRM, sig_alrm);
while (Fgets(sendline, MAXLINE, fp) != NULL)
{
Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
alarm(5);
if ( (n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL)) < 0)
{
if (errno == EINTR)
fprintf(stderr, "socket timeout\n");
else
err_sys("recvfrom error");
}
else
{
alarm(0);
recvline[n] = 0; /* null terminate */
Fputs(recvline, stdout);
}
}
}
示例11: fopen
void interaction::traceFname() {
FILE* fFname = fopen(ipHostFname,"r");
if(fFname==NULL) {
sys_err("call to fopen() failed");
}
// read till the end of file
char* pch;
const char* delim=" ";
while(Fgets(ipHostFname, BUFSIZE, fFname)==1) {
//---------------------------------
// remove '\n' at the end of string
//---------------------------------
ipHostFname[strlen(ipHostFname)-1]=0;
pch = strtok(ipHostFname,delim);
//--------------------------------------------------------
//This magic number 'ipHostFname+strlen(ipHostFname)+1' is
//the address of strings followed 'traceroute'
//--------------------------------------------------------
char* magic = ipHostFname+strlen(ipHostFname)+1;
if(strcmp(pch,"traceroute")==0) {
// check if traceroute 'me'
if(strcmp(magic,"me")==0) {
traceMe();
} else {
traceIpHost(magic);
}
} else {
dprintf(connfd, "INVALID INPUT in file: %s %s", ipHostFname, magic);
}
}
fclose(fFname);
}
示例12: server
void
server(int readfd, int writefd)
{
FILE *fp;
ssize_t n;
struct mymesg mesg;
/* 4read pathname from IPC channel */
mesg.mesg_type = 1;
if ( (n = Mesg_recv(readfd, &mesg)) == 0)
err_quit("pathname missing");
mesg.mesg_data[n] = '\0'; /* null terminate pathname */
if ( (fp = fopen(mesg.mesg_data, "r")) == NULL) {
/* 4error: must tell client */
snprintf(mesg.mesg_data + n, sizeof(mesg.mesg_data) - n,
": can't open, %s\n", strerror(errno));
mesg.mesg_len = strlen(mesg.mesg_data);
Mesg_send(writefd, &mesg);
} else {
/* 4fopen succeeded: copy file to IPC channel */
while (Fgets(mesg.mesg_data, MAXMESGDATA, fp) != NULL) {
mesg.mesg_len = strlen(mesg.mesg_data);
Mesg_send(writefd, &mesg);
}
Fclose(fp);
}
/* 4send a 0-length message to signify the end */
mesg.mesg_len = 0;
Mesg_send(writefd, &mesg);
}
示例13: dg_cli
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
int n;
const int on = 1;
char sendline[MAXLINE], recvline[MAXLINE + 1];
socklen_t len;
struct sockaddr *preply_addr;
preply_addr = (struct sockaddr *)Malloc(servlen);
Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
Signal(SIGALRM, recvfrom_alarm);
while (Fgets(sendline, MAXLINE, fp) != NULL)
{
Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
alarm(5);
for ( ; ; )
{
if (sigsetjmp(jmpbuf, 1) != 0)
break;
len = servlen;
n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
recvline[n] = 0;
printf("from %s: %s", (char *)Sock_ntop_host(preply_addr, len), recvline);
}
}
free(preply_addr);
}
示例14: dg_cli
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
int icmpfd, maxfdp1;
char sendline[MAXLINE], recvline[MAXLINE + 1];
fd_set rset;
ssize_t n;
struct timeval tv;
struct icmpd_err icmpd_err;
struct sockaddr_un sun;
Sock_bind_wild(sockfd, pservaddr->sa_family);
icmpfd = Socket(AF_LOCAL, SOCK_STREAM, 0);
sun.sun_family = AF_LOCAL;
strcpy(sun.sun_path, ICMPD_PATH);
Connect(icmpfd, (SA *)&sun, sizeof(sun));
Write_fd(icmpfd, "1", 1, sockfd);
n = Read(icmpfd, recvline, 1);
if (n != 1 || recvline[0] != '1')
err_quit("error creating icmp socket, n = %d, char = %c",
n, recvline[0]);
FD_ZERO(&rset);
maxfdp1 = max(sockfd, icmpfd) + 1;
while (Fgets(sendline, MAXLINE, fp) != NULL) {
Sendto(sockfd, sendline, strlen(sendline), 0,
pservaddr, servlen);
tv.tv_sec = 5;
tv.tv_usec = 0;
FD_SET(sockfd, &rset);
FD_SET(icmpfd, &rset);
if ((n = Select(maxfdp1, &rset, NULL, NULL, &tv)) == 0) {
fprintf(stderr, "socket timeout\n");
continue;
}
if (FD_ISSET(sockfd, &rset)) {
n = Recvfrom(sockfd, recvline, MAXLINE, 0,
NULL, NULL);
recvline[n] = 0;
Fputs(recvline, stdout);
}
if (FD_ISSET(icmpfd, &rset)) {
if ((n = Read(icmpfd, &icmpd_err,
sizeof(icmpd_err))) == 0)
err_quit("ICMP daemon terminate");
else if (n != sizeof(icmpd_err))
err_quit("n = %d, expected %d",
n, sizeof(icmpd_err));
printf("ICMP error: dest = %s, %s, type = %d,"
" code = %d\n",
Sock_ntop((SA *)&icmpd_err.icmpd_dest,
icmpd_err.icmpd_len),
strerror(icmpd_err.icmpd_errno),
icmpd_err.icmpd_type,
icmpd_err.icmpd_code);
}
}
}
示例15: readcommand
void readcommand()
{
// Allocate userpass char array
int i;
for(i = 0; i < 128; i++)
{
execcom[i] = malloc(128);
}
FILE *file;
char line[128];
i = 0;
file = fopen("rrshcommands.txt", "r");
if (file)
{
while (Fgets(line, sizeof(line), file) != NULL)
{
strtok(line, "\n");
strcpy(execcom[i], line);
i++;
}
fclose(file);
}
}