本文介绍了打印新行到一个文本文件,而无需在Windows回车(CR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我C语言编写一个程序,打印一个随机的十六进制值到一个文本文件中。打印的值具有一个换行(LF)沿一个回车(CR)。然而,CR(在记事本中可见++)是用于文件时造成的问题。有没有一种方法来打印一个新行只用LF和CR没有

这是code:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
无效的主要(){
  INT hexa_address,numberofAddress;
  跟踪文件的char [50]; //文件的名称。
  INT seq_or_rand; // 1随机地址; 2顺序地址  函数srand(时间(NULL)); //重置随机的值  的printf(这发生器产生32位十六进制\\ n \\ n);
  的printf(名称的文件:);
  scanf函数(%S,跟踪文件);
  输出(随机(1)或顺序(2):);
  scanf函数(%d个,&安培; seq_or_rand);  FILE *文件;
  文件= FOPEN(跟踪文件,W); //创建文件  如果(seq_or_rand == 1){//随机发生器
    文件= FOPEN(跟踪文件,W); //创建文件
    对于(numberofAddress = 0; numberofAddress< 10000; numberofAddress ++){//创建10000地址
        //如果(numberofAddress!= 0)        fprintf中(文件,0);
        INT空间= 0;        对于(空间;空间< 8;空间++){//从左边删除任何0
            hexa_address =兰特()%16;
            如果(hexa_address!= 0){
                fprintf中(文件,%X,hexa_address);
                空间++;
                打破;
            }
            否则,如果(hexa_address == 0安培;&放大器;空间== 7){//在00000000条件
                fprintf中(文件,0);
                空间++;
            }
        }        对于(空间;空间< 8;空间++){//继续产生剩余地址
            hexa_address =兰特()%16;
            fprintf中(文件,%X,hexa_address);
        }
        如果(numberofAddress!= 99999)
            fprintf中(文件,\\ t的); //开始一个新行,但不是最后一个
    }
  }
  否则如果(seq_or_rand == 2){//顺序发生器
    文件= FOPEN(跟踪文件,W); //创建文件
    对于(numberofAddress = 0; numberofAddress< 10000; numberofAddress ++){//创建10000地址
        如果(numberofAddress!= 0)
            fprintf中(文件,\\ n); //开始一个新行,但不是第一个
        fprintf中(文件,0);
        fprintf中(文件,%X,numberofAddress * 4);    }
  }  其他{//无效输入
      的printf(无效输入);
      出口(1);
      }    FCLOSE(文件); //完成
}


解决方案

打开/创建二进制模式文件

 文件= FOPEN(跟踪文件,WB);
                          ^二进制模式

否则

  fprintf中(文件,\\ n); / *在文本模式下,该将两个字符。 * /

I am writing a program in C that prints a random hexadecimal value to a text file. The printed value has a carriage return (CR) along with a line feed (LF). However, the CR (visible in notepad++) is causing issues when the file is used. Is there a way to print a new line with just the LF and no CR.

This is the code:

#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
}
解决方案

Open/create the file in binary mode

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

Otherwise

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

这篇关于打印新行到一个文本文件,而无需在Windows回车(CR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 17:48