小编sup*_*ame的帖子

未填充数组(C++)中的最后一个条目是什么?

我把C++放在C#中,因为我刚开始使用C#而且我不确定是否存在差异.

如果你声明一个数组

char arr[10] 
Run Code Online (Sandbox Code Playgroud)

并填写arr [0]到arr [8]的值,将在arr [9]中输入什么值?

空间 ' '?结束'\n'?'\ 0'?或者它什么都没有?

我问这个是因为我总是使用这样的战术

char word[20];
for(count = 0 ; count < 20 ; count++)
{
  cout << word[count];
}
Run Code Online (Sandbox Code Playgroud)

打印一个数组的全部内容,我想知道我是否可以通过使用这样的东西来简化它(例如,如果最后一个条目是'\ 0')

char word[20];
while(word[count] != '\0')
{
  cout << word[count];
}
Run Code Online (Sandbox Code Playgroud)

这样,如果没有填满所有空格,我就不必记住在数组中输入了多少条数据.

如果您知道更快的方式,请告诉我.我倾向于在数组上犯一堆错误.

c# arrays

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

Pthreads:分段错误错误

我有几个问题.这是我第一次尝试制作多线程程序.

注意 - 完整程序位于页面底部

(编译,使用

g++ -pthread -o <executable file name> <sourcefile>.cpp -fpermissive
Run Code Online (Sandbox Code Playgroud)

)

我使用Ubuntu Studio 10.10 64位编译它.

这个程序最大的问题是它给了我一个分段错误.

它似乎是由我在int main()中注释的行引起的.如果我评论该行,它不会给我一个分段错误错误.

为方便起见,这里仅使用int main():

int main()
{
    pthread_attr_t attr;
    pthread_t threads[30];

    /* Initialize mutex and condition variable objects */
    pthread_mutex_init(&direction_mutex, NULL); 
    pthread_mutex_init(&arrive_mutex,NULL);


    pthread_cond_init (&count_threshold, NULL); 
    pthread_cond_init(&arrive_done, NULL);

    /*
     For portability, explicitly create threads in a joinable state

     I'll take your word for it on that one.
     */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

   for( int x = 0 ; x < 30 ; x++) …
Run Code Online (Sandbox Code Playgroud)

c++ gcc posix mutex pthreads

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

为什么这个数据流在字节26结束?

一周前我还在研究那个位图I/O问题.我再次陷入困境,所以我决定从一种熟悉的I/OI开始,并使其更像我需要的稳定(一次检查每个字节(像素)并输出到基于该文件的文件字节的值).

我开始使用一个程序来读取和检查文本文件的每个字符,如果它高于某个阈值则输出"Z",如果它低于某个阈值则输出"A".

该程序运行良好,因此我决定将其从字符更改为文件中的字节.

现在,我一直遇到问题.文件的前26个(字节0-25)字节是正确的,但其余的是0或-1,这取决于我是否使用ifstream.get()ifstream.read.

输入文件Input.FILE是在十六进制编辑器中生成的,只包含0x00-0xFF.它的长度为256个字节.

码:

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
   ifstream sourcefile;
   ofstream output;
   ofstream output2;

   output.open("output.txt");
   output2.open("messages.txt");
   sourcefile.open("Input.FILE");

   vector<char> data(256);
   sourcefile.read(&data[0],256);
   sourcefile.seekg(0,ios::beg);

   for(int i = (int) 0x00 ; i < data.size() ; i++)
   {
      output2 << "Value data[" << i << "] = " << (int) data[i] << endl;

      if((int)data[i] < 0)
      {
         // since I can't make an unsigned char vector, I …
Run Code Online (Sandbox Code Playgroud)

c++ fstream vector

2
推荐指数
3
解决办法
659
查看次数

将十六进制数据输出到文件的正确方法是什么?

我已经读到了[ostream] << hex << 0x[hex value],但我有一些问题

(1)我将我的文件流定义output为十六进制输出文件流output.open("BWhite.bmp",ios::binary);,因为我这样做,是否会使操作中的hex参数变为output<<冗余?

(2)如果我有一个整数值,我想存储在文件中,我使用了这个:

int i = 0;
output << i;
Run Code Online (Sandbox Code Playgroud)

我会存储在小端或大端吗?根据执行或编译程序的计算机,endi-ness是否会发生变化?

此值的大小是否取决于它运行的计算机?我需要使用hex参数吗?

(3)有没有办法将原始十六进制数字输出到文件?如果我希望文件具有十六进制数字43,我应该使用什么?

output << 0x43output << hex << 0x43输出ASCII 4,然后输出ASCII 3.

输出这些十六进制数字的目的是为.bmp文件生成标题.

c++ hex fstream bmp

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

标签 统计

c++ ×3

fstream ×2

arrays ×1

bmp ×1

c# ×1

gcc ×1

hex ×1

mutex ×1

posix ×1

pthreads ×1

vector ×1