我编写了以下代码来逐字符地打印段落,间隔为0.3秒.但是当我编译并运行它时,它会打印出句子中的所有内容.为什么纳秒功能不起作用?
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i = 0;
struct timespec t1, t2;
t1.tv_sec = 0;
t1.tv_nsec = 300000000L;
char story[] = {"I want to print this story / letter by letter on the screen./"};
while(story[i] != '\0') {
if(story[i] == '/')
sleep(1);
else
printf("%c", story[i]);
nanosleep(&t1, &t2);
i++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是一些java代码.
public class SomeClass {
private Connection connection;
public SomeClass(Connection c) {
connection = c;
}
public void someWork(){
Connection c;
try {
// do something
} catch (Exception e) {
// some exception code
} finally {
if (conn != null){
try {c.close();} catch (Exception e) {}
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
但我不喜欢代码
if (conn != null){
try {c.close();} catch (Exception e) {}
}
Run Code Online (Sandbox Code Playgroud)
所以我认为代码
...catch (Exception e) {
// some exception code
} finally {
c = null;
} …Run Code Online (Sandbox Code Playgroud) 我无法让system()在字符串变量中运行命令.
ostringstream convert;
convert << getSeconds(hours);
string seconds = convert.str(); /* converts the output of 'getSeconds()' into
a string and puts it into 'seconds' */
string cmd = "shutdown /s /t " + seconds;
system(cmd);
Run Code Online (Sandbox Code Playgroud)
getSeconds()只需要一个小时的int,将其转换为秒并以秒为单位返回一个int.一切都运行良好,没有错误,直到它到达system(cmd);.编译器然后吐出这个错误:
error: cannot convert 'std::string {aka std::basic_string<char>}' to
'const char*' for argument '1' to 'int system(const char*)'
Run Code Online (Sandbox Code Playgroud)
这是我的包括:
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
Run Code Online (Sandbox Code Playgroud) 从API开始,方法write(int byte)应该采用表示字节的int,这样当EOF出现时它可以返回-1.但是可以做以下事情:
FileOutputStream fi = new FileOutputStream(file);
fi.write(100000);
Run Code Online (Sandbox Code Playgroud)
我希望编译不会超过字节范围.JVM如何解释它?提前致谢.
我正在寻找一种方法来使用C中的唯一void函数读取文件和stdin流.我正在尝试使用此功能:
#define ENTER 10 //'\n' ASCII code
........
void read(FILE *stream, char *string) {
char c;
int counter = 0;
do {
c = fgetc(stream);
string = realloc(string, (counter+1) * sizeof(char));
string[counter++] = c;
} while(c != ENTER && !feof(stream));
string[counter-1] = '\0';
}
Run Code Online (Sandbox Code Playgroud)
但它只适用于stdin流.当我使用文本文件时,文件内容在函数外部不可见.我正在调用这个函数:
read(stdin, inputString);
read(inputFile, fileContent);
Run Code Online (Sandbox Code Playgroud)
并且仅在第一种情况下正常工作.
PS:最初,inputString的fileContent已被声明为
char *inputString = malloc(sizeof(char));
char *fileContent = malloc(sizeof(char));
Run Code Online (Sandbox Code Playgroud)
我知道fgetc返回int,char重新分配的char很昂贵(但我只需要使用必要的内存)而EOF或'\n'存储在字符串中(但后面用0-terminator替换).
我目前有一个nginx服务器设置与rtmp插件的视频流.我正在使用ffmpeg进行流式传输,然后使用adobe flash player在Web浏览器上显示流.
我有成功的视频流,但我无法获得音频.
这是我用来启动流的ffmpeg命令
ffmpeg -f video4linux2 -s 320x240 -r 16 -i /dev/video0 -f alsa -i hw:0 -an -f flv rtmp://123.456.789.51:31002/rover/mystream
Run Code Online (Sandbox Code Playgroud)
然后流转到运行在此conf文件上的nginx
worker_processes 1;
error_log logs/user/error.log debug;
events {
worker_connections 1024;
}
rtmp {
server {
listen 31002;
application rover {
live on;
#meta copy;
#interleave on;
allow publish all;
allow play all;
hls on;
#record keyframes;
#record_path /tmp;
#record_max_size 128K;
#record_interval 30s;
#record_suffix .this.is.flv;
#on_publish http://localhost:8080/publish;
#on_play http://localhost:8080/play;
#on_record_done http://localhost:8080/record_done;
}
}
}
http {
server {
listen …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的错误写入MapPath中的文本文件中.但我有一个问题是:
path = Server.MapPath("~/emails.txt");
Run Code Online (Sandbox Code Playgroud)
实际上Server是在System.Web.UI.Page命名空间中我想在我的cs文件中使用它没有aspx文件...我的意思是它是AppCode中的CodeFile; 如何从cs CodeFile写入文件?
我已经编写了一个readInt()方法来读取System.in中的整数,但由于某种原因,返回的每个整数都是10.这不管数字是一位还是几位而且让我感到困惑.我的代码在下面,我哪里出错了?
/**
* @return The next integer read from the Input Stream
*/
public static int readInt() throws IOException {
BufferedInputStream in = new BufferedInputStream(System.in);
char c = (char) in.read();
int num = 0, cNum = Character.getNumericValue(c);
//Checks that the current value of c is in fact a single digit number (as it must be to be a char and an int)
while (cNum > -1 && cNum < 10) {
num = num * 10 + cNum;
c = …Run Code Online (Sandbox Code Playgroud) 类似于20个小时的事情已经过去了,我仍然不会想要得到什么问题.
首先,这些参数有主要功能.
char ***matrix;
int line, maxCollumn, i, j;
int lineData[100];
Run Code Online (Sandbox Code Playgroud)
然后我正在调用该函数
PutFirstAndLast(&matrix, &line, &maxCollumn, lineData);
Run Code Online (Sandbox Code Playgroud)
此函数用于动态分配我的矩阵并返回其行数和列数.lineData用于存储我在每行上有多少项,因为稍后在函数中我将用""填充"空"空格.
我的麻烦功能有这个标题
int PutFirstAndLast(char**** matrixPointer, int *line2, int *maxCollumn2, int *lineArray)
Run Code Online (Sandbox Code Playgroud)
在这个函数中,我将生成一个以半延迟方式"数组"调用的新矩阵,我将为新的"数组"分配内存,以便将每个单词从文件"in.txt"中放入其中.
int PutFirstAndLast(char**** matrixPointer, int *line2, int *maxCollumn2, int *lineArray)
{
FILE *inputFile;
char buffer[100], *p;
char ***array;
int i, j, collumn, maxCollumn = 0, line = -1;
inputFile = fopen("in.txt", "r");
while (fgets(buffer, 100, inputFile))
{
line++;
collumn = 0;
if (line == 0)
array = (char***)malloc(sizeof(char**));
else
array = (char***)realloc(array, sizeof(char**)* …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
internal class Program
{
private static void Main(string[] args)
{
var client = new TcpClient();
client.ConnectAsync("localhost", 7105).Wait();
var stream = client.GetStream();
var observable = stream.ReadDataObservable().Repeat();
var s = from d in observable.Buffer(4)
let headerLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(d.ToArray(), 2))
let b = observable.Take(headerLength)
select b.ToEnumerable().ToArray();
s.Subscribe(a => Console.WriteLine("{0}", a));
Console.ReadLine();
}
}
public static class Extensions
{
public static IObservable<byte> ReadDataObservable(this Stream stream)
{
return Observable.Defer(async () =>
{
var buffer = new byte[1024];
var readBytes = await stream.ReadAsync(buffer, 0, buffer.Length); …Run Code Online (Sandbox Code Playgroud)