在windows中将新行打印到文本文件,而不使用回车符(CR)

在windows中将新行打印到文本文件,而不使用回车符(CR),c,printf,ansi,C,Printf,Ansi,我正在用C编写一个程序,将随机十六进制值打印到文本文件中。打印值具有回车符(CR)和换行符(LF)。但是,在使用该文件时,CR(在记事本++中可见)会导致问题。有没有办法只打印LF而不打印CR的新行 代码如下: #include <stdio.h> #include <stdlib.h> void main(){ int hexa_address, numberofAddress; char tracefile[50]; //name of file in

我正在用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.\n\n");
  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,"%x", 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,"%x", numberofAddress*4);

    }
  }

  else{ //invalid input
      printf("Invalid Input");
      exit(1);
      }

    fclose(file); //done
}
#包括
#包括
void main(){
int hexa_地址,numberofAddress;
char tracefile[50];//文件名
int seq_或_rand;//1表示随机地址;2表示顺序地址
srand(time(NULL));//重置随机变量的值
printf(“此生成器生成32位十六进制。\n\n”);
printf(“文件名:”);
scanf(“%s”,tracefile);
printf(“随机(1)或顺序(2):”;
scanf(“%d”&顺序或随机数);
文件*文件;
file=fopen(tracefile,“w”);//创建文件
如果(seq_或_rand==1){//随机生成器
file=fopen(tracefile,“w”);//创建文件

对于(numberofAddress=0;numberofAddress以二进制模式打开/创建文件

file = fopen(tracefile, "wb");
                          ^ Binary mode
否则

fprintf(file,"\n"); /* In text mode this appends two characters. */

@user968623这将打开文件,如果文件不存在,它将创建一个新文件。非常感谢。我花了很长时间才注意到这一点。