如何在 c++ 中读取文本文件
How to read a text file in c++?
我想检测视频中预定义的特定内容。为此,我分别使用一段已经识别的视频和另一个输入视频。我已经读取了识别的视频中的数据,并且咬合数组保存在文本文件中。现在我想将保存的文本文件的内容与输入的视频内容进行比较。
这是我的代码
void CFfmpegmethods::VideoRead(){
av_register_all();
avformat_network_init();
ifstream inFile;
inFile.open("H:\Sanduni_projects\sample_ad.txt");
const char *url = "H:\Sanduni_projects\ad_1.mp4";
AVDictionary *options = NULL;
AVFormatContext *s = avformat_alloc_context();
AVPacket *pkt = new AVPacket(); //this structure stores compressed data
//open an input stream and read the header
int ret = avformat_open_input(&s, url, NULL,NULL);
//avformat_find_stream_info(s, &options); //finding the missing information
if (ret < 0)
abort();
if (!inFile) {
cerr << "Unable to open file datafile.txt";
exit(1); // call system to stop
}
av_dict_set(&options, "video_size", "640x480", 0);
av_dict_set(&options, "pixel_format", "rgb24", 0);
if (avformat_open_input(&s, url, NULL, &options) < 0){
abort();
}
av_dict_free(&options);
AVDictionaryEntry *e;
if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
fprintf(stderr, "Option %s not recognized by the demuxer.n", e->key);
abort();
}
int i = 1;
int64_t duration = 0;
int size = 0;
uint8_t *data; //Unsigned integer type with a width of exactly 8 bits.
int sum = 0;
int total_size = 0;
int64_t total_duration = 0;
int packet_size = 0;
int64_t stream_index = 0;
int64_t bit_rate = 0;
AVBufferRef *buf;
//writing data to a file
outdata.open("H:\Sanduni_projects\sample_ad.txt");
//outdata.open("H:\Sanduni_projects\log.txt");
if (!outdata){
cerr << "Error: file could not be opened" << endl;
exit(1);
}
//Split what is stored in the file into frames and return one for each call
//returns the next frame of the stream
while(1){
int frame = av_read_frame(s, pkt); //one frame is readed.
if (frame < 0) break;
size = pkt->size;
data = pkt->data;
for (int j = 0; j < size; j++){
for (int k = 0; k < 12; k++){
while (!inFile.eof){
}
//if (data[j] != ) break;
}
}
//int decode = avcodec_send_packet(avctx, pkt);
}
//make the packet free
av_packet_unref(pkt);
delete pkt;
cout << "total size: " << total_size << endl;
cout << "total duration:" << total_duration << endl;
outdata.close();
//Close the file after reading
avformat_close_input(&s);
}
void CFfmpegmethods::SampleAdCreation(){
av_register_all();
const char *url_ref = "H:\Sanduni_projects\sample_ad.mp4";
AVDictionary *options = NULL;
AVDictionary *options_ref = NULL;
AVFormatContext *s = avformat_alloc_context();
AVPacket *pkt = new AVPacket(); //this structure stores compressed data
//open an input stream and read the header
int ret = avformat_open_input(&s, url_ref, NULL, NULL);
if (ret < 0)
abort();
av_dict_set(&options, "video_size", "640x480", 0);
av_dict_set(&options, "pixel_format", "rgb24", 0);
if (avformat_open_input(&s, url_ref, NULL, &options) < 0){
abort();
}
av_dict_free(&options);
AVDictionaryEntry *e;
if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
fprintf(stderr, "Option %s not recognized by the demuxer.n", e->key);
abort();
}
uint8_t *data;
//writing data to a file
outdata.open("H:\Sanduni_projects\sample_ad.txt");
if (!outdata){
cerr << "Error: file could not be opened" << endl;
exit(1);
}
for (int m = 0; m < 12; m++){
int size = pkt->size;
int frame = av_read_frame(s, pkt);
if (frame < 0) break;
data = pkt->data;
for (int j = 0; j < size; j++){
outdata << data[j];
} outdata<<endl;
}
//make the packet free
av_packet_unref(pkt);
delete pkt;
outdata.close();
//Close the file after reading
avformat_close_input(&s);
}
当你输出你的outdata
时,你应该用空格分隔字节:
for (int j = 0; j < size; j++){
outdata << data[j] << ' ';
}
否则,字节 {11, 1} 将产生与 {1, 11} 相同的输出。
然后,您可以从inFile中读取这些字节:
unsigned char dataByte;
while (inFile >> dataByte)
{
do something with dataByte;
}
相关文章:
- 将从格式化文本文件读取的文本数据存储到链表
- 读取字符串文本输入以创建 2D 矢量
- 需要从存档中读取一行文本.txt直到找到"hhh",然后转到下一行
- 反转从控制台读取的文本
- 高效读取 3MB 文本文件,包括解析
- C++程序在 freopen 中读取大文本后停止工作
- C++忽略使用字符串流进行读取的文本文件中的空白
- 读取大量文本/文本文件的更快方法?
- 使用 std::ifstream 读取 ASCII 文本文件C++
- 如何读取从文本文件到几个数组的不同类型的数据?C
- 使用标准C++无线流读取UTF-8文本并转换为UTF-16
- 从二进制文件中抓取文本时,为什么 xdg_vtnr=8 是我的结果
- 视觉读取列文本数据并在C++中放入 3D 数组
- C++在同一函数中读取各种文本文件
- 从进程内存中读取的文本块中丢弃解析的行
- 读取长文本文件时程序崩溃 - "*.exe has stopped working"
- IO:从一个C++程序和另一个Java程序同时写入和读取同一个文本文件
- OpenCV,在显示图像时读取用户文本输入(使用 waitKey)
- 如何逐行枚举从文件中读取的文本,并找到 C 中字符和单词最多的行
- P/Invoke in ASP.NET (从 dll 读取/写入文本文件)