69,382
社区成员
发帖
与我相关
我的任务
分享
/*
jernymy use strtok and atoi to printf all int value from a file
*/
#include "stdio.h"
#include "stdlib.h" // atoi
#include "string.h" // strtok
#define SOURCE_FILE "tmp.txt" // jernymy source file
#define TOKEN_SYMBOL " " // the symbol now is " "
#define LINE_NUM 128 // every line have string number
#define ARRAY_NUM 128*3 // int value have total arrray, need / 3
#define MAX_INT_LEN 12 // max int number char length+'0', 0xFFFFFFFF->4294967295
int main(void)
{
int nIdx = 0;
int nCount = 0;
char *pchToken = NULL;
char achBuf[LINE_NUM] = {0};
int anData[ARRAY_NUM] = {0};
FILE *fpSrc = fopen(SOURCE_FILE, "rt");
if (NULL == fpSrc)
{
printf("open %s fail, exit!\n", SOURCE_FILE);
return -1;
}
while (!feof(fpSrc))
{
// jernymy read one line from a file
fgets(achBuf, LINE_NUM, fpSrc);
// jernymy find the " " in the case
pchToken = strtok(achBuf, TOKEN_SYMBOL);
while (NULL != pchToken)
{
// switch char array to int value
anData[nCount++] = atoi((const char *)pchToken);
pchToken = strtok(NULL, TOKEN_SYMBOL);
}
}
for (nIdx = 0; nIdx < nCount; nIdx++)
{
if (0 == nIdx%3)
{
printf("\n");
}
printf("%d ", anData[nIdx]);
}
return 0;
}
#include<iostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> result;
copy(istream_iterator<int>(ifstream("E:\\source\\VC9\\study\\Debug\\1.txt")),istream_iterator<int>(),back_inserter(result));
copy(result.begin(),result.end(),ostream_iterator<int>(cout,"\n"));
}
while(fscanf("%d %d %d",&x,&y,&z)
{
}
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main()
{
FILE* fp=NULL;
char temp[100],fileName[]="data.txt",a[10],b[10],c[10];
int d[MAX],i=0,j;
fp = fopen(fileName,"r");
if(NULL == fp){
printf("打开文件失败!\n");
return 1;
}else{
fgets(temp,MAX,fp);
while(!feof(fp))
{
sscanf(temp,"%s%s%s",a,b,c);
d[i++] = atoi(a);
d[i++] = atoi(b);
d[i++] = atoi(c);
fgets(temp,MAX,fp);
}
printf("解析文件成功!\n");
for(j=0; j<i; j++){
if(j%3==0) printf("\n");
printf("%d\t",d[j]);
}
fclose(fp);
system("pause");
}
}
FILE *pfile=fopen("c:\\Data.txt","r");
char buf[50]={0};
char tem[50]={0};
while(fgets(buf,50,pfile) != NULL && buf[0] !='\n')
{
int i=0;
if (buf[i] != ' ')
{
int j=0;
tem[j]=buf[i];
i++;
j++;
}
m_nCx=atoi(tem);
memset(tem,0,50);//读入x坐标
if (buf[i+1] != ' ')
{
int j=0;
tem[j]=buf[i+1];
i++;
j++;
}
m_nCy=atoi(tem);
/*MessageBox(tem);*/
memset(tem,0,50);//读入y坐标
strcpy(tem,&buf[i+2]);
m_nRGB=(COLORREF)atoi(tem);//读入RGB值
memset(buf,0,strlen(buf));