小编眠りネ*_*ネロク的帖子

在 Linux 中从 .txt 文件重定向到可执行文件

我有一个文本文件,其中包含从 1 到 1000 ( numbers.txt) 的所有数字,我有一个可执行文件 ( ex2-1),当它获取从 1 到 1000 的所有数字时(它将数字作为输入一个一个),它会打印“完成! ”。当您运行该文件时,您会看到:

please insert 1:
Run Code Online (Sandbox Code Playgroud)

如果您输入 1,它会显示相同但带有 2,如果不是,它将打印“错误输入”并关闭。

我知道如何逐行读取文本文件:

#!/bin/bash
filename='numbers.txt'
while read line; do
echo "$line" #echo is just to show where the number is being saved
done < $filename
Run Code Online (Sandbox Code Playgroud)

但是有什么方法可以重定向,而不是打印到屏幕上,而是转到可执行文件?

linux bash

2
推荐指数
1
解决办法
63
查看次数

为什么在main中隐含声明函数?C编程

#define在main中使用之前声明了该函数,但我仍然得到:

隐含的函数fakultet声明...

答案应该是6 ...

#include <stdio.h>

#define fakultet(x) ((x)>1?((x)*(fakultet(x-1))):(1))

int main(void) {
    printf(fakultet(3));
}
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
85
查看次数

从二进制文件写入和读取不起作用

所以,我想把9个数字写入二进制文件,然后从该文件中读取它们并将它们打印到屏幕上,但由于某种原因它不起作用,我无法弄清楚为什么.我试过在互联网上寻找答案,但似乎没有任何帮助.如果有人能指出我的代码中的错误(这可能非常明显,但我是c的新手),我会非常感激.这是代码:

int main()
 {
    FILE *f;
    if(f=fopen("dat", "wb") == NULL){
      return 1;
    }
   int c;
   for(int i = 0; i < 9; i++){
     c = fwrite(&i, sizeof(int), 1, f);
     printf("%d",c); //it prints 0 every single time, as if nothing was written in the file
  }
  fclose(f);
  if(f=fopen("dat", "rb+")==NULL){
    return 1;
  }
  int a;
  while(fread(&a, sizeof(int), 1, f)){
    printf("%d\n", a);
  }
  fclose(f);
}
Run Code Online (Sandbox Code Playgroud)

c binaryfiles

1
推荐指数
1
解决办法
66
查看次数

模板什么时候写尖括号(&lt;...&gt;)?

给出这个示例类模板:

\n\n
template<typename T>\nclass Stack {\n    T * data;\n    int size;\n    int nextIndex;\npublic:\n    Stack(int size = 100);\n    Stack(const Stack& stack);\n    ~Stack();\n    Stack& operator=(const Stack& s);\n    void push(const T& t);\n    void pop();\n    T& top();\n    const T& top() const;\n    int getSize() const;\n\n    class Full {\n    };\n    class Empty {\n    };\n};\n\ntemplate<typename T>\nvoid Stack::push(const T& t) {\n    if (nextIndex >= size) {\n        throw Full();\n    }\n    data[nextIndex++] = t;\n}\n\xc2\xa0\ntemplate<typename T>\nvoid Stack::pop() {\n    if (nextIndex <= 0) {\n        throw Empty();\n    }\n    nextIndex--;\n}  \n
Run Code Online (Sandbox Code Playgroud)\n\n

push和方法的实现部分可以吗 …

c++ templates class

1
推荐指数
1
解决办法
646
查看次数

strchr()函数实现的问题

我最近开始研究汇编代码,我正在尝试重新编码一些基本的系统函数来掌握它,我目前仍然在我的0x0上遇到了一个分段错误strchr.

section .text
global strchr

strchr:
    xor rax, rax

loop:
    cmp BYTE [rdi + rax], 0
    jz end

    cmp sil, 0
    jz end

    cmp BYTE [rdi + rax], sil
    jz good

    inc rax
    jmp loop

good:
    mov rax, [rdi + rcx]
    ret

end:
    mov rax, 0
    ret
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚如何使用GDB调试它,我遇到的文档非常有限或难以理解.

我在C中使用以下主要测试

extern char *strchr(const char *s, int c);

int main () {
   const char str[] = "random.string";
   const char ch = '.';
   char *ret;

   ret = strchr(str, ch);
   printf("%s\n", ret); …
Run Code Online (Sandbox Code Playgroud)

unix assembly x86-64

1
推荐指数
1
解决办法
160
查看次数

如何查看向量中的内容?

当我检查homework使用向量的内部值时cout,似乎只返回内部的值homework[0]。有人可以查看我的代码并让我知道我要去哪里了吗?

int main()
{
    cout << "Please enter midterm and final: " << endl;
    double midterm, gfinal;
    cin >> midterm >> gfinal;
    cout << "Enter all your homework grades, " << endl;

    double x;

    cin >> x;

    // initing a vector object named homework
    vector<double> homework;
    // show debug on the vector
    while (homework.size() != 3)
        homework.push_back(x);
    if (homework.size() == 0)
    {
        cout << endl << "You need to enter at least one number"; …
Run Code Online (Sandbox Code Playgroud)

c++ cout vector

1
推荐指数
1
解决办法
146
查看次数

如何理解“ vector &lt;int&gt; avector(arr,arr + sizeof(arr)/ sizeof(arr [0]))”?

在下面的代码中,什么是

vector<int> avector (arr, arr + sizeof(arr) / sizeof(arr[0]) );
Run Code Online (Sandbox Code Playgroud)

main()

vector<int> bubbleSort(vector<int> avector) { //the vector for bubble sort
  for (int passnum = avector.size()-1; passnum > 0; passnum -= 1) {
      for (int i = 0; i < passnum; i++) {
          if (avector[i] > avector[i+1]) {
              int temp = avector[i];
              avector[i] = avector[i+1];
              avector[i+1] = temp;
          }
      }
  }
  return avector;
}

int main() {
    // Vector initialized using a static array
    static const int arr[] = …
Run Code Online (Sandbox Code Playgroud)

c++ arrays stl vector

1
推荐指数
2
解决办法
117
查看次数

是否有一个类似于STL的函数用索引的某些函数填充数组?

在C ++中,我们具有类似std::fill或的函数,std::fill_n这些函数是方便的单行方式,以指针数组,向量,std::arrays和其他容器填充值。一些容器也有其自己的fill方法以允许填充恒定值。还有函数std::generate{_n}std::iota,前者允许使用生成器函数填充元素,后者则用索引填充范围。

我正在寻找的是一种类似的解决方案-最好是一种单线解决方案,并在标准库中定义-该解决方案允许使用某些索引功能填充容器。例如,这将是数组的解决方案:

std::array<int, 100> arr;
for (std::size_t i = 0; i < 100; i++)
    arr[i] = f(i);
Run Code Online (Sandbox Code Playgroud)

f(std::size_t i)索引的某些功能在哪里。

有整合的方法吗?

c++ stl c++11

1
推荐指数
1
解决办法
110
查看次数

Movable but non-copyable objects: passing by value vs by reference?

Considering only objects that are movable but non-copyable (e.g., std::thread or std::unique_ptr), I want to transfer the ownership of the resource such an object contains by passing it as an argument to a constructor. I'm comparing two approaches: the constructor taking the object by value vs. by rvalue reference.

As an example with std::thread, consider the following class Value whose constructor takes an std::thread by value:

#include <thread>
#include <utility>

struct Value {
   Value(std::thread th): th_(std::move(th)) …
Run Code Online (Sandbox Code Playgroud)

c++ ownership-semantics parameter-passing move-semantics c++11

1
推荐指数
1
解决办法
68
查看次数

Base constructor calls derived constructor depending on input - Choose object sub-type at runtime

Let's say I have a Human class:

class Human {
public:
    bool isFemale;
    double height;
    Human(bool isFemale, double height) {
        this->isFemale = isFemale;
        this->height = height;
    }
};
Run Code Online (Sandbox Code Playgroud)

and derived classes, such as Female and Male, which implement their own methods. Do I have a way, in C++11, to determine at runtime, depending on the inputs into the Human constructor, which "sub-type" (Male or Female) Human should be? I am putting different behaviour for Male and Female in their respective …

c++ oop design-patterns factory c++11

1
推荐指数
1
解决办法
112
查看次数