0

我正在通过 Linux 上的 C++ 将一长串浮点数打印到 .csv 文件中。

我需要在一行上打印 5000 个数字而没有任何中断。每个数字用逗号分隔。

但是,在打印出 512 个数字后,该行被中断,以“^M”结尾,然后转到换行符。但是,我从未在代码中添加换行符。

我的代码是:

 ofstream("MyFile.CSV")

我也试过:

 ofstream("MyFile.CSV", std::ios_base::app)

它们都不起作用。我在 EXCEL 和 VIM 中打开它。

搜索后,我找不到有用的解决方案。

任何帮助将不胜感激。

谢谢

4

1 回答 1

0

从下面的程序开始,看看行为是否不同。还可以考虑使用“wc”之类的工具来获得真正的行数,而不是相信编辑器的行为。我对 C++ 的了解远远落后于时代,但我相信那些新奇的字符串会和我心爱的人一样好用char *

#include <fstream>

using namespace std;

int main() {
        ofstream out("stackoverflow-13957937.csv");

        const char *separator = "";
        const char *comma = ",";

        for(int i=0; i<1000; i++) {
                out << separator;
                out << i*1.1f;
                separator = comma;
        }
        out << endl;
        return 0;
}

这个程序将输出 1000 个浮点数,用逗号分隔,到一行。

chet@baconator:~$ g++ -Wall stackoverflow-13957937.cc -o stackoverflow-13957937
chet@baconator:~$ ./stackoverflow-13957937
chet@baconator:~$ cat stackoverflow-13957937.csv
    0,1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,11,12.1,13.2,14.3,15.4,16.5,17.6,18.7,19​​.8,20.9,22,23.1,24.2,25.3,26.4, 27.5,28.6,29.7,30.8,31.9,33,34.1,35.2,36.3,37.4,38.5,39.6,40.7,41.8,42.9,44,45.1,46.2,47.3,48.4,49.5,50.6,51.7,52.8,53.9, 55,56.1,57.2,58.3,59.4,60.5,61.6,62.7,63.8,64.9,66,67.1,68.2,69.3,70.4,71.5,72.6,73.7,74.8,75.9,77,78.1,79.2,80.3,81.4, 82.5,83.6,84.7,85.8,86.9,88,89.1,90.2,91.3,92.4,93.5,94.6,95.7,96.8,97.9,99,100.1,101.2,102.3,103.4,104.5,105.6,106.7,107.8,106.7,107.8,10.1 112.2,113.3,114.4,115.5,116.6,117.7,118.8,119.9,121,122.1,123.2,124.3,125.4,126.5,127.6,128.7,129.8,130.9,132,133.1,134.2,135.3,136.4,137.5,138.6,139.7,140.8, 141.9,143,144.1,145.2,146.3,147.4,148.5,149.6,150.7,151.8,152.9,154,155.1,156.2,157.3,158.4,159.5,160.6,161.7,162.8,163.9,165,166.1,167.2,168.3,169.4,170.5,171.6, 172.7,173.8,174.9,176,177.1,178.2,179.3,180.4,181.5,182.6,183.7,184.8,185.9,187,187,188.1,189.2,190.3,191.4,192.5,192.5,193.6,194.7,19​​4.7,19​​5.8,95.8,196.96,198,198,198,199.199.199.199.200.2,20.20.220.20.2,20.20.20.20.20.2,20.20.20.20.20.2 in 212.3,213.4,214.5,215.6,216.7,217.8,8,218.9,9,2220,221.1,222.222.22.22,222,2233.3,3,3,3,225.5,5,5,5,5,226.6,2227.7,7,7,7,228.8,8,8,8,2228.8,22228.8,222229.9,2239.9,239.9,2332.233.9,233.9,232.232.22,223.9.9,232.232.232.22,223.9.9,232.232.233.9mt 242,243.1,244.2,245.3,246.4,247.5,248.6,249.7,250.8,251.9,253,254.1,255.2,256.3,257.4,258.5,259.6,260.7,261.8,262.9,264,265.1,266.2,267.3,268.4,269.5,270.6,271.7, 272.8,273.9,275,276.1,277.2,2,278.3,27999.4,280.5,281.5,281.6,282.6,282.7,283.83.8,84.9,284.9,286,287.1,288.2,289.39.39.39.39.399.399.399.399.399.3999.399.b 303.6,304.7,305.8,306.9,308,309.1,310.2,311.3,312.4,313.5,314.6,315.7,316.8,317.9,319,320.1,321.2,322.3,323.4,324.5,325.6,326.7,327.8,328.9,330,331.1,332.2,333.3, 334.4,335.5,336.6,337.7,338.8,339.9,341,342.1,343.2,344.3,345.4,346.5,347.6,348.7,349.8,350.9,352,353.1,354.2,355.3,356.4,357.5,358.6,359.7,360.8,361.9,363,364.1,365.2,366.3,367.4,368.5,369.6,370.7,371.8,372.9, 374,375.1,376.2,377.3,378.4,379.5,380.6,381.7,382.8,383.9,385,386.1,387.2,388.3,389.4,390.5,391.6,392.7,393.8,394.9,396,397.1,398.2,399.3,400.4,401.5,402.6,403.7, 404.8,405.9,407,408.1,409.2,410.3,411.4,412.5,413.6,414.7,415.8,416.9,418,419.1,420.2,421.3,422.4,423.5,424.6,425.7,426.8,427.9,429,430.1,431.2,432.3,433.4,434.5, 435.6,436.7,437.8,438.9,440,441.1,442.2,443.3,444.4,445.5,446.6,447.7,448.8,449.9,451,452.1,453.2,454.3,455.4,456.5,457.6,458.7,459.8,460.9,462,463.1,464.2,465.3, 466.4,467.5,468.6,469.7,470.8,471.9,473,474.1,475.2,476.3,477.4,478.5,479.6,480.7,481.8,482.9,484,485.1,486.2,487.3,488.4,489.5,490.6,491.7,492.8,493.9,495,496.1, 497.2,498.3,499.4,500.5,501.6,502.7,503.8,504.9,506,507.1,508.2,509.3,510.4,511.512.6,513.7,514.514.8,515.9,517,517,518.18.1,519.299.2,520.3,521.4,4,522.522.522.5223.6,522.524.7,525525525525525255255255555555555555555555559,59,59,59,59,59,59,55555559,59,55555555555555555太平值536.8,537.9,539,540.1,541.2,542.3,543.3,543.4,544.545.6,545.6,546.7,7,547,54.8,54.9,9,550,1,1,55555.5555.555555555555555555555555555555555555,5555555,555555555,55555555,55555555,555555555555555太平值567.6,568.7,569.8,570.9,572,573.1,574.2,575.3,576.4,577.5,578.6,579.7,580.8,581.9,583,584.1,585.2,586.3,587.4,588.5,589.6,590.7,591.8,592.9,594,595.1,596.2,597.3, 598.4,599.5,600.6,601.7,602.8,603.9,605,606.1,607.2,608.3,609.4,610.5,611.6,612.7,613.8,614.9,616,617.1,618.2,619.3,620.4,621.5,622.6,623.7,624.8,625.9,627,628.1, 629.2,630.3,631.4,632.5,633.6,634.7,635.8,636.9,638,639.1,640.2,641.3,642.4,643.5,644.6,645.7,646.8,647.9,649,650.1,651.2,652.3,653.4,654.5,655.6,656.7,657.8, 658.9,660,661.1,662.2,663.3,664.4,665.5,666.6,667.7,668.8,669.9,671,672.1,673.2,674.3,675.4,676.5,677.6,678.7,679.8,680.9,682,683.1,684.2,685.3,686.4,687.5,688.6,689.7,690.8,691.9,693,694.1,695.2,696.3,697.4,698.5,699.6, 700.7,701.8,702.9,704,705.1,706.2,707.3,708.4,709.5,710.5,710.6,711.6,711.712.8,712.8,715,715,715,716.1717,717,717.29.718.29.719.718.4.719.718.4.719.4. ,719.4.719.718.4.19.4. ,719.4.719.718.4.19.4. ,719.4.719.4.719.4. ,719.4.719.4.719.4. ,719.4.719.4.718.4. ,719.4.719.4.7199.419.419.4.198.4m 731.5,732.6,733.7,734.8,735.9,737,737,738.1,739.2,740.3,741.4,4,74,742.5,743.6,743.6,744.7445.8446.9446.7448,749749.749.749.74.74.279.74.251.279.175.2res 762.3,763.4,764.5,765.6,766.7,767.8,768.9,770,771.1,772.2,773.3,774.4,775.5,776.6,777.7,778.8,779.9,781,782.1,783.2,784.3,785.4,786.5,787.6,788.7,789.8,790.9, 792,793.1,794.2,795.3,796.4,797.5,798.6,799.7,800.8,801.9,803,804.1,805.2,806.3,807.4,808.5,809.6,810.7,811.8,812.9,814,815.1,816.2,817.3,818.4,819.5,820.6,821.7, 822.8,823.9,825,826.1,827.2,828.3,829.4,830.5,831.6,832.7,833.8,834.9,836,837.1,838.2,839.2,839.3,840.4,841.5,842 863.5,864.6,865.7,866.8,867.9,869,870.1,871.2,872.3,873.4,874.5,875.6,876.7,877.8,878.9,880,881.1,882.2,883.3,884.4,885.5,886.6,887.7,888.8,889.9,891,892.1,893.2, 894.3,895.4,896.5,897.6,898.7,899.8,900.9,902,903.1,904.2,905.3,906.4,907.5,908.6,909.7,910.8,911.9,913,914.1,915.2,916.3,917.4,918.5,919.6,920.7,921.8,922.9, 924,925.1,926.2,927.3,928.4,929.5,930.5,930.6,931.7,932.8,932.8,933.933.935,935,936.1,937.2,937.2,938.39393939.4,94,94,94,94,94,94,94,94.94994.994.994,94994.994.9994.994.994.9994.9994. in 954.8,955.9,957,958.1,959.2,960.3,961.4,4,962.5,963.5,963.6,964.7,965.865.8,966.966.968,968,969.1,970.2,970.2,971.39797.97.997.997.99.99.99.99.99.999.99.999.999.99.999.998.,99.99.99.99.99.99.99.99.99.99.999.9999.9999.999.998. ,999.99.99.99.99.99.999.999.99.99.99.9999.9999.9999.999.998.an in 985.6,986.7,987.8,988.9,990,991.1,992.2,993.3,994.4,995.5,996.6,997.7,7,998.8,99999.9,1001,1002.1002.1,1003.2,1004.3,1005.4,1005.4,1006.5,5,1007.6,1008.7,7,7,1009.8,9,8,9,10112,10112,101113.11011.101.101.101,101.101.2019.19.19.19.19.19.19.19.19.19.19.19.19.19.19.19.19.19.,101,101.19.19.19.19.19.19.19.bh 1023,1024.1,1025.2,1026.3,1027.4,1028.5,1029.6,1030.7,1031.8,1032.9,1034,1035.1,1036.2,1037.3,1038.4,1039.5,1040.6,1041.7,1042.8,1043.9,1045,1046.1,1047.2,1048.3,1049.4, 1050.5,1051.6,1052.7,1053.8,1054.9,1056,1057.1,1058.2,1059.3,1060.4,1061.5,1062.6,1063.7,1064.8,1065.9,1067,1068.1,1069.2,1070.3,1071.4,1072.5,1073.6,1074.7,1075.8,1076.9, 1078,1079.1,1080.2,1081.3,1082.4,1083.5,1084.6,1085.7,1086.8,1087.9,1089,1090.1,1091.2,1092.3,1093.4,1094.5,1095.6,1096.7.8,10971031.8,1032.9,1034,1035.1,1036.2,1037.3,1038.4,1039.5,1040.6,1041.7,1042.8,1043.9,1045,1046.1,1047.2,1048.3,1049.4,1050.5,1051.6,1052.7,1053.8,1054.9,1056,1057.1,1058.2, 1059.3,1060.4,1061.5,1062.6,1063.7,1064.8,1065.9,1067,1068.1,1069.2,1070.3,1071.4,1072.5,1073.6,1074.7,1075.8,1076.9,1078,1079.1,1080.2,1081.3,1082.4,1083.5,1084.6,1085.7, 1086.8,1087.9,1089,1090.1,1091.2,1092.3,1093.4,1094.5,1095.6,1096.7,1097.8,1098.91031.8,1032.9,1034,1035.1,1036.2,1037.3,1038.4,1039.5,1040.6,1041.7,1042.8,1043.9,1045,1046.1,1047.2,1048.3,1049.4,1050.5,1051.6,1052.7,1053.8,1054.9,1056,1057.1,1058.2, 1059.3,1060.4,1061.5,1062.6,1063.7,1064.8,1065.9,1067,1068.1,1069.2,1070.3,1071.4,1072.5,1073.6,1074.7,1075.8,1076.9,1078,1079.1,1080.2,1081.3,1082.4,1083.5,1084.6,1085.7, 1086.8,1087.9,1089,1090.1,1091.2,1092.3,1093.4,1094.5,1095.6,1096.7,1097.8,1098.9
chet@baconator:~$ wc stackoverflow-13957937.csv
   1 1 5790 stackoverflow-13957937.csv
切特@培根:~$

请注意,wc输出是行数 (1)、字数 (1) 和字节数 (5790)。

从技术上讲,行数包括换行符,因此如果最后一行没有以换行符结尾,那么它将报告的行数比真实行数少一。

但是,如果文件没有以换行符结尾,那么后面的提示cat stackoverflow-13957937.csv将在同一行。如下图所示:

chet@baconator:~$ echo -n '例子--> '
示例--> chet@baconator:~$
于 2012-12-19T17:58:10.613 回答