在下面的程序中,write()在写入文件时返回-1.
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main() {
int fd_r=0,fd_w=0;
int w_ret=100;
fd_r = open("reader.txt", O_RDONLY);
fd_w = open("writer.txt",O_CREAT,S_IRWXU);
char *buf = (char *)malloc(50);
while(read(fd_r,buf,30))
{
w_ret = write(fd_w,buf,30);
printf("%d", w_ret);
}
}
Run Code Online (Sandbox Code Playgroud)
问题:我无法调试为什么会发生这种情况.关于如何调试此类问题的代码和建议的更正非常受欢迎
我有以下程序
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
int x = 1;
void ouch(int sig) {
printf("OUCH! dividing by zero!\n");
x = 0;
}
void fpe(int sig) {
printf("FPE! I got a signal: %d\n",sig);
psignal(sig, "psignal");
x = 1;
}
int main(void) {
(void) signal(SIGINT, ouch);
(void) signal(SIGFPE, fpe);
while(1)
{
printf("Hello World: %d\n",1/x);
sleep(1);
}
}
Run Code Online (Sandbox Code Playgroud)
问题:执行此程序时 - 当我从终端向程序提供SIGINT时 - ""OUCH!除以零!"输出 - 按预期.下一条消息是"FPE!我得到一个信号:8 psignal:浮点异常".这个消息继续 - 不停止.我的疑问是在调用fpe信号处理程序后,我将x设置为1.我因此期望Hello World应该是显示在输出中.
以下是我得到的输出的记录:
Hello World: 1
Hello World: 1
^COUCH! dividing by zero!
FPE! I …Run Code Online (Sandbox Code Playgroud) 我们可以编写一个在C++中不使用预处理器的可移植包含保护程序吗?如果是这样,怎么办呢?
我写了一个小程序,以了解c ++ 0x中的期货是如何工作的.在运行代码时,我得到一个错误,如"错误:'printEn'未在此范围内声明".我无法理解问题是什么.请指出我在这里做错了什么,如果可能的话,写相同的正确代码..
#include <future>
#include <iostream>
using namespace std;
int printFn()
{
for(int i = 0; i < 100; i++)
{
cout << "thread " << i << endl;
}
return 1;
}
int main()
{
future<int> the_answer2=async(printEn);
future<int> the_answer1=async(printEn);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
在运行程序时对printFn进行更改后,我收到错误消息"找不到版本`GLIBCXX_3.4.14'(./a.out要求)".这表明了什么?
如何提取" 马修从" ":6/MKTCzMS/YU:mtjoseph.10974:10060:马修 "
下面的函数接受一个char指针数组Eg:arr [0]:ls arr [1]: - l arr [2]: - a arr [3]:/ etc arr [4]:NULL/*因为execvp需要最后是一个NULL*/
// function call is runCmd(arr);
函数定义如下:
void runCmd(char *arr[]){
pid_t child_pid,tpid;
int child_status;
child_pid = fork();
if(child_pid == 0){
/* The child process executes the exec*/
execvp(arr[0],arr);
/*if it returns it must have failed */
fflush(stdout);
printf("Unknown Command \n");
exit(0);
}
else {
/* let the parent wait for the child */
do{
tpid = wait(&child_status);
}while(tpid != child_pid);
}
}
Run Code Online (Sandbox Code Playgroud)
执行后我收到消息 -
ls: cannot …Run Code Online (Sandbox Code Playgroud) 我有以下程序(DGRAM套接字)来从时间服务器检索时间.该程序不会按预期打印出当前时间.
#define DAYTIME_SERVER_PORT 13
int main(int argc, char *argv[])
{
int connectionFd, in;
struct sockaddr_in servaddr;
char buf[BUFSIZ];
connectionFd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(DAYTIME_SERVER_PORT);
inet_pton(AF_INET,"time.mit.edu", &servaddr.sin_addr);
sendto( connectionFd,buf ,1, 0,(struct sockaddr_in *)&servaddr, sizeof(servaddr) );
in = recv(connectionFd, buf, BUFSIZ, 0 ) ;
buf[in] = 0 ;
printf("Time is %s \n",buf);
close(connectionFd);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是 - 我在哪里弄错了?任何代码更改和宝贵的建议表示赞赏.我不知道如何使用recvform函数,因此尝试了recv.我连接的日间服务器是time.mit.edu