如何把下面一组数据写入TXT 文件中

l1005834871 2014-04-23 04:01:05
0 : 0;
1 : 0;
2 : 0;
3 : 0;
4 : 0;
5 : 80;
6 : 380;
7 : 380;
8 : 380;
9 : 280;
...全文
220 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
一根烂笔头 2014-04-25
  • 打赏
  • 举报
回复
引用 11 楼 braveboyny 的回复:
[quote=引用 8 楼 my_live_123 的回复:]

/* fwrite example : write buffer */
#include <stdio.h>

int main ()
{
  FILE * pFile;
  char buffer[] = { 'x' , 'y' , 'z' };
  pFile = fopen ("myfile.bin", "wb");
  fwrite (buffer , sizeof(char), sizeof(buffer), pFile);
  fclose (pFile);
  return 0;
}

/* fprintf example */
#include <stdio.h>

int main ()
{
   FILE * pFile;
   int n;
   char name [100];

   pFile = fopen ("myfile.txt","w");
   for (n=0 ; n<3 ; n++)
   {
     puts ("please, enter a name: ");
     gets (name);
     fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
   }
   fclose (pFile);

   return 0;
}

/* fputs example */
#include <stdio.h>

int main ()
{
   FILE * pFile;
   char sentence [256];

   printf ("Enter sentence to append: ");
   fgets (sentence,256,stdin);
   pFile = fopen ("mylog.txt","a");
   fputs (sentence,pFile);
   fclose (pFile);
   return 0;
}
gets()这函数现在已被禁用了哦,超级危险的函数[/quote] 恩,晓得就好
l1005834871 2014-04-24
  • 打赏
  • 举报
回复
还有其他的 存储方式吗
l1005834871 2014-04-24
  • 打赏
  • 举报
回复
0 : 0; 1 : 0; 2 : 0; 3 : 0; 4 : 0; 5 : 80; 6 : 380; 7 : 380; 8 : 380; 9 : 280; 上述字符串 除了 我之前把他们串接 成一个字符串 还有其他的方式吗
不败的拿破仑 2014-04-24
  • 打赏
  • 举报
回复
LZ你敢一次把问题说清楚吗?你是在让LS这么多人陪你玩吗
l1005834871 2014-04-24
  • 打赏
  • 举报
回复
“0 : a0b” 中的 a0b 原本就是 0xa0b 我是想把 “0 : a0b” 如何一行一行的 存储在一个 字符串数组里面 比如 {“0 : a0b”,“1 : 0”,“2 : 7000”,“3 : 4000”} 这个数组该 如何定义 或者可以定义成 其他 ......
tbwork 2014-04-24
  • 打赏
  • 举报
回复
引用 13 楼 l1005834871 的回复:
其实我不明白是 如何把 上述的 打印文件 的每一行 如何存储在一个 数组里面 0 : a0b 1 : 0 2 : 7000 3 : 4000 4 : 9000 5 : 0 6 : 80 7 : 180 8 : 1f80 在上述的代码中 我只是把所有的字符串 全部串成了一个大的字符串
一行一行的读,(

string str[SIZE];
for(int i=0;i<SIZE;i++)
file1>>str; 
如果需要存成int数组, 只需要解析字符串,比如0 : a0b 根据冒号分为两个子串, 0 a0b 由于是16进制,可能需要自己写个转换为10进制函数。 如 a = 'a'-'a'+10; b= 'b'-'a' +10; 总结下就是: x = 'x' is 字母? 'x'-'a'+10 : strtoint('x');
l1005834871 2014-04-24
  • 打赏
  • 举报
回复
其实我不明白是 如何把 上述的 打印文件 的每一行 如何存储在一个 数组里面 0 : a0b 1 : 0 2 : 7000 3 : 4000 4 : 9000 5 : 0 6 : 80 7 : 180 8 : 1f80 在上述的代码中 我只是把所有的字符串 全部串成了一个大的字符串
l1005834871 2014-04-24
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

unsigned char shuzi[64]=
{0xa,0xb,0x00,0x00,0x70,0x00,0x40,0x00,0x90,0x00,0x00,0x00,0x00,0x80,0x01,0x80,
0x1F,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,
0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,
0x01,0x80,0x03,0xC0,0x1F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned int N16_32[32];


void main()
{
int i,j,sum;
FILE *fp;
int i_1,j_1;
char *string[32]={"","","","",""};
char strinn[4];
char stringn[288]={"\0"};
char strin1[4];

//char stringm[6]={'1','2','3','a','b','\0'};
char stringm[6]={"sdfgh"};


for(i_1=0,j_1=0;i_1<64,j_1<32;i_1++,j_1++)
{
N16_32[j_1] =(shuzi[i_1]<<8)|shuzi[++i_1];

// printf("\n%x : %x;\n",j_1,N16_32[j_1]);
}

for(i=0;i<32;i++)
{
itoa(N16_32[i], strinn, 16);
itoa(i, strin1, 16);
if(i==0)
{
strcpy(stringn,strin1);
strcat(stringn," : ");
strcat(stringn,strinn);
strcat(stringn,"\n");

}
else
{
strcat(stringn,strin1);
strcat(stringn," : ");
strcat(stringn,strinn);
strcat(stringn,"\n");

}

if(i==31)
printf("%s",stringn);
}


// printf("%s",stringn);

fp=fopen("d:\\MYFILE.txt","w");

if(fwrite(stringn,sizeof(char),288,fp)!=1)
printf("写入错误\n");

fclose(fp);

}
braveboyny 2014-04-23
  • 打赏
  • 举报
回复
引用 8 楼 my_live_123 的回复:

/* fwrite example : write buffer */
#include <stdio.h>

int main ()
{
  FILE * pFile;
  char buffer[] = { 'x' , 'y' , 'z' };
  pFile = fopen ("myfile.bin", "wb");
  fwrite (buffer , sizeof(char), sizeof(buffer), pFile);
  fclose (pFile);
  return 0;
}

/* fprintf example */
#include <stdio.h>

int main ()
{
   FILE * pFile;
   int n;
   char name [100];

   pFile = fopen ("myfile.txt","w");
   for (n=0 ; n<3 ; n++)
   {
     puts ("please, enter a name: ");
     gets (name);
     fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
   }
   fclose (pFile);

   return 0;
}

/* fputs example */
#include <stdio.h>

int main ()
{
   FILE * pFile;
   char sentence [256];

   printf ("Enter sentence to append: ");
   fgets (sentence,256,stdin);
   pFile = fopen ("mylog.txt","a");
   fputs (sentence,pFile);
   fclose (pFile);
   return 0;
}
gets()这函数现在已被禁用了哦,超级危险的函数
自信男孩 2014-04-23
  • 打赏
  • 举报
回复
有规则的格式化的字符串,建议使用fscanf和fprintf进行读写;并且这两个函数是以字符形式写入的,而不是二进制形式;写入后可以通过打开看到。
挣扎中前行 2014-04-23
  • 打赏
  • 举报
回复
引用 5 楼 braveboyny 的回复:
[quote=引用 4 楼 zhao4zhong1 的回复:] fprintf
别忘了在后面加fflush[/quote] ++
一根烂笔头 2014-04-23
  • 打赏
  • 举报
回复

/* fwrite example : write buffer */
#include <stdio.h>

int main ()
{
  FILE * pFile;
  char buffer[] = { 'x' , 'y' , 'z' };
  pFile = fopen ("myfile.bin", "wb");
  fwrite (buffer , sizeof(char), sizeof(buffer), pFile);
  fclose (pFile);
  return 0;
}

/* fprintf example */
#include <stdio.h>

int main ()
{
   FILE * pFile;
   int n;
   char name [100];

   pFile = fopen ("myfile.txt","w");
   for (n=0 ; n<3 ; n++)
   {
     puts ("please, enter a name: ");
     gets (name);
     fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
   }
   fclose (pFile);

   return 0;
}

/* fputs example */
#include <stdio.h>

int main ()
{
   FILE * pFile;
   char sentence [256];

   printf ("Enter sentence to append: ");
   fgets (sentence,256,stdin);
   pFile = fopen ("mylog.txt","a");
   fputs (sentence,pFile);
   fclose (pFile);
   return 0;
}
一根烂笔头 2014-04-23
  • 打赏
  • 举报
回复
ctrl + A->ctrl +C -》打开notebook-》ctrl+V,最后ctrl+S
tbwork 2014-04-23
  • 打赏
  • 举报
回复

#include<iostream>
#include<fstream>
using namespace std;
#define DATA_SIZE 1000
void main()
{
  ofstream file1("c:/data.txt");
 for(int i=0;i<DATA_SIZE ;i++)
   file1<<"0: 1200;"<<endl;
 file1.close();
}
braveboyny 2014-04-23
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
fprintf
别忘了在后面加fflush
赵4老师 2014-04-23
  • 打赏
  • 举报
回复
fprintf
l1005834871 2014-04-23
  • 打赏
  • 举报
回复
不只这几个数 应该有个几万个数吧
罗博士 2014-04-23
  • 打赏
  • 举报
回复
就这么点数?不要用C语言了,复制粘贴吧。 如果是为了练习,就依次 fopen(); fwrite() fclose()
cy2015yc 2014-04-23
  • 打赏
  • 举报
回复
fopen() fwrite() fclose() 或者用ofstream

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧