简体   繁体   English

如何检查空行?

[英]How To Check empty Lines?

my input.txt file: 我的input.txt文件:

4

4

5 0 1 2
1 4 0 0
1 1 5 4
0 6 3 2

0 4 1 2
1 7 5 0
2 3 5 6
0 6 2 2

1:0 4 2 0

my program for now: 我现在的程序:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <sstream>
using namespace std;

int main()
{
ifstream File("input.txt");
string line;
string num;
string array[50];
string comma;
int i=0;
    while (getline(File,line)) {

        comma="";
        istringstream s(line);

        if (line.empty()){

            comma=",";
            s >> num ;
            array[i] =  comma;
        }

        else {
            s >> num ;
            array[i] =  num;
        }
        i++;

    }        
return 0;
}

well, my program is not giving me what I want! 好吧,我的程序没有给我我想要的东西! when I print the array[i] its giving me only the numbers in the first Column! 当我打印array [i]时,只给我第一列中的数字! like this : 像这样 :

4
,
4
,
5
3
1
7
,
0
1
2
6
,
1:0

what i want to do is to put a comma where ever there is an empty line , so I can distinguish between these numbers and store them inside an intger array to do mathematical operations between them . 我想做的是在任何空行处放置一个逗号,这样我就可以区分这些数字并将它们存储在一个整数数组中,以便在它们之间进行数学运算

to explain my input.txt file: 解释我的input.txt文件:

numbers and matrix size inside input file can be changed by me. 输入文件中的数字和矩阵大小可以由我更改。

4 <= #number of items 

4 <= #types of items

5 0 1 2 <= matrix #1
1 4 0 0
1 1 5 4
0 6 3 2

0 4 1 2 <= matrix #2
1 7 5 0
2 3 5 6
0 6 2 2

1:0 4 2 0 <= Available numbers for item #1 

and I want to : 我想:

  1. store number of items inside a variable. 在变量中存储项目数。

  2. store types of items inside a variable. 将项目类型存储在变量中。

  3. store matrix #1 and matrix #2 inside a an 2 arrays and do Subtraction between matrix #1 and matrix #2. 矩阵#1矩阵#2存储2个数组中,然后在矩阵#1和矩阵#2之间进行减法 运算。

Can this be done ? 能做到吗? or is there any easier method to distinguish between these numbers and store them inside variables and an inter array ? 或者有没有更简单的方法来区分这些数字并将它们存储在变量和内部数组中?

The problem is simple. 问题很简单。

You read a line of data with 您使用以下命令读取了一行数据

while (getline(File,line)) {

But then you only process one element from that line: 但是然后您只处理该行中的一个元素:

    if (line.empty()) {
        ...
        s >> num ;
        ...
    }
    else {
        s >> num ;
    }

You simply need to put that if ... else statement into some kind of loop. 您只需要将if ... else语句放入某种循环中即可。

Additionally, I don't understand the first branch of that if statement: if line is empty, there shouldn't be anything in s to read from. 另外,我不理解该if语句的第一个分支:如果line为空,则s不应该读取任何内容。

So, slightly edited, this should be closer to what you're looking for (I haven't tried this to see if it actually works): 因此,稍作修改,它应该更接近您要查找的内容(我没有尝试过查看它是否确实有效):

    ...
    while (getline(File,line)) {
        if (line.empty()) {
            array[i] = ","
            ++i;
            continue;
        }
        istringstream s(line);

        while(s >> num) {
            array[i] =  num;
            ++i;
        }
    }        
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM