从文件中填充2D数组

时间:2017-04-06 23:34:49

标签: c++ arrays

您好我从文件填充2D数组有问题。

我的文件数组中包含字符串数组 每行的字符串都有78个字符 和文件有19行。 (最后一行有只有60个字符的字符串)

此档案

def get_instagram_posts(name, time, coordinates):
    max_time_dt = time + timedelta(hours=3)
    min_time_dt = time - timedelta(hours=1)

    milli_max_time = int(calendar.timegm(max_time_dt.timetuple()))
    milli_min_time = int(calendar.timegm(min_time_dt.timetuple()))

    max_time = str(int(milli_max_time/1000))
    min_time = str(int(milli_min_time/1000))

    # Time has to be in seconds (no milliseconds).
    # See http://stackoverflow.com/questions/21091543/ajax-call-to-instagram-get-media-search-timestamp-issue

    dist_rad_str = str(insta_dist_radius_m)
    count_str = str(insta_count)

    api = InstagramAPI(access_token=insta_access_token, 
        client_secret=insta_client_secret)

    # https://www.instagram.com/developer/endpoints/media/

    r = api.media_search(name, count_str, str(coordinates[0]),
        str(coordinates[1]), min_time, max_time)

    print(r)

我尝试用文件中的变量填充2D数组。 我需要5个ROWS和300个COLUMN的填充数组

但我不知道怎么做。

它有关图像中像素的信息。我希望用C ++填充数组信息。数组必须具有类似x * y的图像。 (5 * 300)

我尝试了几个小时但不成功。

我的代码在这里。

00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FFFFFFFFFFXX
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1XX1FFFFFFFFFFF
FFFFFFFFF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FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

你有什么想法吗?感谢

//或者你有如何从文件中读取字符串,如没有行的ONE BIG字符串?

1 个答案:

答案 0 :(得分:0)

使用istream::get逐个字符地读取文件可能更容易,并忽略换行符,例如

#include <fstream>
#include <cctype>
#include <iostream>

bool isHex(char val) {
  return ('0' <= val && val <= '9') || ('A' <= val && val <= 'F');
}

void readFile(std::ifstream& file, char** imageHex, int rows, int columns) {
  char currentByte;

  for (int row =0; row < rows; row++ ) {
    for(int column=0; column < columns; column++ ) {
      if (!file.get(currentByte)) {
          // end of file, or other error
          return;
      }
      if (isHex(currentByte)) {
        imageHex[row][column]=currentByte;
      }
      else if (!std::isspace(currentByte)) {
         std::cout << "Unexpected character '" << currentByte 
         << "' at (" << row << ", " <<column << ") " << std::endl;
      }
    }
  }
}