我试图逐行将一些数据写入文件,然后从源文件逐行分别读取每个数据并将其写入另一个文件。 但是在将数据写入目标文件时,我得到了空间。 我的意思是数据要在多个空格之后写入。
我使用fgets而不是fread。
/*
* ======== fatsd.c ========
*/
#include <file.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <third_party/fatfs/ffcio.h>
#include <ti/display/Display.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SDFatFS.h>
#include <ti/drivers/UART.h>
/* Example/Board Header files */
#include "Board.h"
/* Buffer size used for the file copy process */
#ifndef CPY_BUFF_SIZE
#define CPY_BUFF_SIZE 2048
#endif
/* String conversion macro */
#define STR_(n) #n
#define STR(n) STR_(n)
/* Drive number used for FatFs */
#define DRIVE_NUM 0
int i= 20;
int x;
char con_buff[100];
const char inputfile[] = "fat:"STR(DRIVE_NUM)":input1.txt";
const char outputfile[] = "fat:"STR(DRIVE_NUM)":output.txt";
const char copyfile[] = "fat:"STR(DRIVE_NUM)":copy2.txt";
char textarray[] = "Jan 1 2017 00:00:00 75 822 96 548 85 76 82 93 78 82 64 89";
char text[]="ram sham heloo bye";
char cpy[]="\r\n";
static Display_Handle display;
/* File name prefix for this filesystem for use with TI C RTS */
char fatfsPrefix[] = "fat";
//unsigned char cpy_buff[CPY_BUFF_SIZE + 1];
char cpy_buff[CPY_BUFF_SIZE + 1];
char mybuff[CPY_BUFF_SIZE + 1];
/*
* ======== mainThread ========
* Thread to perform a file copy
*
* Thread tries to open an existing file inputfile[]. If the file doesn't
* exist, create one and write some known content into it.
* The contents of the inputfile[] are then copied to an output file
* outputfile[]. Once completed, the contents of the output file are
* printed onto the system console (stdout).
*/
void *mainThread(void *arg0)
{
SDFatFS_Handle sdfatfsHandle;
/* Variables for the CIO functions */
FILE *src, *dst,*new;
/* Variables to keep track of the file copy progress */
unsigned int bytesRead = 0;
unsigned int bytesWritten = 0;
unsigned int filesize;
unsigned int totalBytesCopied = 0;
/* Call driver init functions */
GPIO_init();
SDFatFS_init();
/* Configure the LED pin */
GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
/* add_device() should be called once and is used for all media types */
add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read,
ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename);
/* Turn on user LED */
GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
strcpy(con_buff,"This example requires a FAT filesystem on the SD card.\r\n"
"You will get errors if your SD card is not formatted with a filesystem.\r\n");
puts(con_buff);
/* Mount and register the SD Card */
sdfatfsHandle = SDFatFS_open(Board_SDFatFS0, DRIVE_NUM);
if (sdfatfsHandle == NULL)
{
strcpy(con_buff,"Error starting the SD card\n");
puts(con_buff);
x=1;
while (1);
}
else
{
strcpy(con_buff,"Drive is mounted\n");
x=2;
}
/* Try to open the source file */
src = fopen(inputfile, "a");
if (!src)
{
strcpy(con_buff,"Creating a new file ...");
puts(con_buff);
x=3;
/* Open file for both reading and writing */
src = fopen(inputfile, "w+");
if (!src)
{
strcpy(con_buff,"Error:could not be created.\nPlease check the Board.html if additional jumpers are necessary.check if sd card is inserted correctly\n"
"Aborting... \n");
puts(con_buff);
x=4;
while (1);
}
x=5;
/* Reset the internal file pointer */
rewind(src);
}
else {
fseek(src, 0, SEEK_END);
filesize = ftell(src);
if(filesize!=0)
{
fwrite(cpy, 1, strlen(cpy), src);
}
fwrite(textarray, 1, strlen(textarray), src);
fwrite(cpy, 1, strlen(cpy), src);
fwrite(text, 1, strlen(text), src);
fwrite(cpy, 1, strlen(cpy), src);
fwrite(textarray, 1, strlen(textarray), src);
fwrite(cpy, 1, strlen(cpy), src);
fwrite(text, 1, strlen(text), src);
x=6;
}
fflush(src);
fclose(src);
/* Create a new file object for the file copy */
dst = fopen(outputfile, "w");
new = fopen(copyfile, "a");
src = fopen(inputfile, "r");
if(!new)
{
strcpy(con_buff,"Error opening new file ");
puts(con_buff);
}
if (!dst)
{
strcpy(con_buff,"Error opening \"%s\"\n"
"Aborting...\n");
puts(con_buff);
x=7;
while (1);
}
else {
strcpy(con_buff,"Starting file copy\n");
puts(con_buff);
x=8;
}
/* Copy the contents from the src to the dst */
while (!feof(src))
{
fgets(cpy_buff,sizeof(cpy_buff),src); //READ FROM FILE
/* Read from source file */
puts(cpy_buff);
i=i+1;
fwrite(cpy_buff, 1,sizeof(cpy_buff),dst); //PUBLISH
strcpy(cpy_buff,"\0");
}
fflush(dst);
/* Get the filesize of the source file */
fseek(src, 0, SEEK_END);
filesize = ftell(src);
sprintf(con_buff,"size of file is %d",filesize);
puts(con_buff);
/* Close both inputfile[] and outputfile[] */
fclose(src);
fclose(dst);
fclose(new);
sprintf(con_buff,"File \"%s\" (%u B) copied to \"%s\" (Wrote %u B)\n",inputfile, filesize, outputfile, totalBytesCopied);
puts(con_buff);
/* Now output the outputfile[] contents onto the console */
/* Stopping the SDCard */
SDFatFS_close(sdfatfsHandle);
sprintf(con_buff,"Drive %u unmounted\n", DRIVE_NUM);
puts(con_buff);
return (NULL);
}
/*
* ======== fatfs_getFatTime ========
*/
int32_t fatfs_getFatTime(void)
{
/*
* FatFs uses this API to get the current time in FatTime format. User's
* must implement this function based on their system's timekeeping
* mechanism. See FatFs documentation for details on FatTime format.
*/
/* Jan 1 2017 00:00:00 */
return (0x4A210000);
}
答案 0 :(得分:0)
关于如下语句:
fwrite(cpy_buff, 1,sizeof(cpy_buff),dst);
这将写入整个缓冲区,即使缓冲区cpy_buff
的大多数尾随字节都是垃圾。建议:
fwrite(cpy_buff, 1,strlen(cpy_buff),dst);