在Windows中没有回车符(CR)的情况下将新行打印到文本文件中
发布时间:2020-12-14 02:11:07 所属栏目:Windows 来源:网络整理
导读:我正在用C编写一个程序,它将随机的十六进制值打印到文本文件中.打印值具有回车(CR)和换行(LF).但是,CR(在记事本中可见)在使用文件时会导致问题.有没有办法打印一个只有LF和没有CR的新行. 这是代码: #include stdio.h#include stdlib.hvoid main(){ int hexa
我正在用C编写一个程序,它将随机的十六进制值打印到文本文件中.打印值具有回车(CR)和换行(LF).但是,CR(在记事本中可见)在使用文件时会导致问题.有没有办法打印一个只有LF和没有CR的新行.
这是代码: #include <stdio.h> #include <stdlib.h> void main(){ int hexa_address,numberofAddress; char tracefile[50]; //name of file int seq_or_rand; //1 for random address; 2 for sequential address srand(time(NULL)); //reset the value of the random printf("This generator generates 32bits hexadecimal.nn"); printf("Name of file: "); scanf("%s",tracefile); printf("Random (1) or Sequential (2): "); scanf("%d",&seq_or_rand); FILE *file; file = fopen(tracefile,"w"); // create file if(seq_or_rand == 1){ //random generator file = fopen(tracefile,"w"); // create file for(numberofAddress = 0; numberofAddress<10000;numberofAddress++){ //create 10000 address //if(numberofAddress!=0) fprintf(file,"0 "); int space = 0; for(space; space<8; space++){ //remove any 0 from the left hexa_address = rand() % 16; if(hexa_address != 0){ fprintf(file,"%x",hexa_address); space++; break; } else if(hexa_address == 0 && space == 7){ //in condition of 00000000 fprintf(file,"0"); space++; } } for(space; space<8; space++){ //continue generating the remaining address hexa_address = rand() % 16; fprintf(file,hexa_address); } if(numberofAddress!=99999) fprintf(file,"t"); //start a new line,but not on the last one } } else if(seq_or_rand == 2){ //sequential generator file = fopen(tracefile,"w"); // create file for(numberofAddress = 0; numberofAddress<10000;numberofAddress++){ //create 10000 address if(numberofAddress!=0) fprintf(file,"n"); //start a new line,but not on the first one fprintf(file,"0 "); fprintf(file,numberofAddress*4); } } else{ //invalid input printf("Invalid Input"); exit(1); } fclose(file); //done } 解决方法
以二进制模式打开/创建文件
file = fopen(tracefile,"wb"); ^ Binary mode 除此以外 fprintf(file,"n"); /* In text mode this appends two characters. */ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 17 win7 sp1 x64/VS2015下配置creo4.0二次开发环境——调用
- windows – 如何创建hotfolder解决方案
- windows – 按名称批量文件到网络上的MASS ping计算机组,检
- 蓝牙在2台Windows 7计算机上使用C#进行配对而不进行配对
- windows-server-2008-r2 – 为什么Windows Update突然失败,
- windows 2003服务器定时重启的设置方法
- windows-phone-8 – 是否可以使用HTML5,CSS3和JavaScript开
- signalr – Microsoft.Owin类似OwinMiddleware和IOwinConte
- windows-phone-7 – 适用于Windows Phone的RTMP库
- 批处理文件 – 在Windows中批处理Pandoc转换