嗨,所以这段代码在5分钟前起作用了,完全可以满足我的需要。
数据为:
public List<Error> LeerArchivo()
{
List<Error> listaErrores = new List<Error>();
string @folderLocation = StrfolderOut + "\\fileOut.txt";
Console.WriteLine(@folderLocation);
//OpenFileDialog openFileDialog = new OpenFileDialog();
//openFileDialog.ShowDialog();
if (File.Exists(@folderLocation))
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(@folderLocation))
{
string line = "";
// Read and display lines from the file until the end of
// the file is reached.
Console.WriteLine("reading");
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
Console.WriteLine(line);
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
else
{
Console.WriteLine("no existe");
}
return listaErrores;
}
我的代码是:
Set Field [G],Sheet Resistivity (Gavg) [ohm/sqr]
0.0000E+0,
0.0000E+0,7.270620E+2
1.0000E-2,
1.0000E-2,7.271280E+2
-1.0000E-2,
-1.0000E-2,
-1.0000E-2,7.271290E+2
就像我在5分钟前说的那样,这行之有效,它摆脱了带有空y字符串的行,并且一切都很好。但是后来我又回到了原始数据文件,但在那里没有用。我很困惑,所以我将原始数据放入了data.txt中,但现在两者都不起作用。坦白说,我很困惑,但是最好的条件是,如果循环使它生效,那么这是可行的。
答案 0 :(得分:1)
听起来您可能有一些空白。我将使用this answer中的解决方案来修剪y
字符串中的空白,以确保:
#include <iostream>
#include <algorithm>
#include <cctype>
#include <locale>
#include <fstream>
using namespace std;
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !std::isspace(ch);
}));
}
int main() {
[...]
while(getline(ip,x,',')){
getline(ip,y,'\n');
ltrim(y);
if(y!="")
std::cout <<x<<","<< y << '\n';
}
}