我正在通过Linux上的C ++将大量浮点数输出到.csv文件中。
我需要在一行上连续打印5000个数字。每个数字都用逗号分隔。
但是,在打印出512个数字后,该行断开了,以“ ^ M”结尾,然后转到换行符。但是,我从未在代码中添加换行符。
我的代码是:
ofstream("MyFile.CSV")
我也尝试过:
ofstream("MyFile.CSV", std::ios_base::app)
他们都不工作。我在EXCEL和VIM中打开了它。
搜索后,我找不到有用的解决方案。
任何帮助将不胜感激。
谢谢
最佳答案
从下面的程序开始,看看行为是否不同。还可以考虑使用“ 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,108.9,110,111.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,188.1,189.2,190.3,191.4,192.5,193.6,194.7,195.8,196.9,198,199.1,200.2,201.3,202.4, 203.5,204.6,205.7,206.8,207.9 209,210.1,211.2,212.3,213.4,214.5,215.6,216.7,217.8,218.9,220,221.1,222.2,223.3,224.4,225.5,226.6,227.7,228.8,229.9,231,232.1,233.2,234.3,235.4,236.5,236.5,237.6,238.7 ,239.8,240.9,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,278.3,279.4,280.5,281.6,282.7,283.8,284.9,286,287.1,288.2,289.3,290.4,291.5,292.6,293.7,294.8,295.9,297,298.1,299.2,300.3 ,301.4,302.5,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,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,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,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,488.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.5,512.6,513.7,514.8,515.9,517,518.1,519.2,520.3 ,521.4,522.5,523.6,524.7,525.8,526.9,528,529.1,530.2,531.3,532.4,533.5,534.6,535.7,536.8,537.9,539,540.1,541.2,542.3,543.4,544.5,545.6,546.7,547.8,548.9,550,551.1 ,552.2,553.3,554.4,555.5,556.6,557.7,558.8,559.9,561,562.1,563.2,564.3,565.4,566.5,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,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,709.5 ,710.6,711.7,712.8,713.9,715,716.1,717.2,718.3,719.4,720.5,721.6,722.7,723.8,724.9,726,727.1,728.2,729.3,730.4,731.5,732.6,733.7,734.8,735.9,737,738.1,739.2,740.3 ,741.4,742.5,743.6,744.7,745.8,746.9,748,749.1,750.2,751.3,752.4,753.5,754.6,755.7,756.8,757.9,759,760.1,761.2,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,7 77.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.3,840.4,841.5,842.6,843.7,844.8,845.9,847,848.1,849.2,850.3,851.4,852.5,853.6,854.7,855.8,856.9,858,859.1,860.2,861.3,862.4,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,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.6,931.7,932.8,933.9,935,936.1,937.2,938.3,939.4,940.5,941.6,942.7,943.8,944.9,946,947.1,948.2,949.3,950.4,951.5,952.6,953.7,954.8,955.9,957,958.1,959.2,960.3, 961.4,962.5,963.6,964.7,965.8,966 .9,968,969.1,970.2,971.3,972.4,973.5,974.6,975.7,976.8,977.9,979,980.1,981.2,982.3,983.4,984.5,985.6,986.7,987.8,988.9,990,991.1,992.2,993.3,994.4,995.5,996.6,997.7 ,998.8,999.9,1001,1002.1,1003.2,1004.3,1005.4,1006.5,1007.6,1008.7,1009.8,1010.9,1012,1013.1,1014.2,1015.3,1016.4,1017.5,1018.6,1019.7,1020.8,1021.9,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,1097.8,1098.9
chet @ baconator:〜$ wc stackoverflow-13957937.csv
1 1 5790 stackoverflow-13957937.csv
chet @ baconator:〜$
请注意,
wc
输出是对行(1),字(1)和字节(5790)的计数。从技术上讲,行数包括换行符,因此,如果最后一行未以换行符结尾,则报告的行数将少于实际行数。
但是,如果文件未以换行结尾,则
cat stackoverflow-13957937.csv
之后的提示将在同一行。如下图所示:chet @ baconator:〜$ echo -n'例子->'
示例-> chet @ baconator:〜$