如何输出复数文件?

jacksonsnake 2008-07-21 12:06:00
需要把buffer[32][i]里面32行数据的每一行数据分别写进32个不同的txt文件里,每行的长度不同,应该如何输出文件名?

FILE *fp=NULL;
char filename[256];

for(k=0; k<32; k++)
{
.................
.................

...全文
277 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jacksonsnake 2008-07-21
  • 打赏
  • 举报
回复
我就把
temp = itoa(i+1, temp, 10);/* integer to array */
Outfile = strcat(Outfile, temp);/* copy the file number to the end of the path */
Outfile = strcat(Outfile, word);/* set the file as txt file */
去掉就能成功debug~~~~~
ChamPagneZ 2008-07-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 aozhi 的回复:]
1楼忘了fclose
厚厚
[/Quote]
里面错误太多了,还没缩进.
能说明问题就好了.哈哈
aozhi 2008-07-21
  • 打赏
  • 举报
回复
1楼忘了fclose
厚厚
ChamPagneZ 2008-07-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jacksonsnake 的回复:]
是比如说第一行写进一个新的文档文件“data 1.txt”,第二行写进“data 2.txt”,第i行写进“data i.txt”
[/Quote]
我贴的这个应该就是你要的这种吧.
ChamPagneZ 2008-07-21
  • 打赏
  • 举报
回复
因为我自己测试过了才发给你.
你的Main函数太大了,你可以单步一下看下,至少,你函数执行成功的条件下都没有设置返回呢.
你的错误信息能贴出来看看么?
jacksonsnake 2008-07-21
  • 打赏
  • 举报
回复
还是不行啊!
就是那行报错

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define FILENAME_SIZE 512
#define LINE_SIZE 512
#define DATA_SIZE 20000
//#define FILENAME "sequence.txt"

int main()
{
int i = 0;
int k = 0;
int m = 0;
int n = 0;
int r = 0;
int a = 0;
int gcd = 0;
int lcm = 0;
int column = 0;
int row = 0;
int count = 0;
int Length1 = 0;
int Length2 = 0;
int this_line_is_data;
int degree = 0;
int addresult =0;
char word[] = ".txt";
char temp[100];
char Infile1[FILENAME_SIZE];
char Infile2[FILENAME_SIZE];
char Outfile[FILENAME_SIZE];
char buf[LINE_SIZE];
int sequence[200][200];
int matrix[200][200];
int data1[DATA_SIZE];
int data2[DATA_SIZE];
FILE *fp = NULL;
char *ptr = NULL;

//Read the first sequence
printf("\nEnter first file path\n");/* appoint file */
gets(Infile1);
fp = fopen(Infile1, "r");/* open file */
if (fp == NULL)
{
printf("Failed to open file %s\n", Infile1);
return 1;
}

while (1)
{
if (fgets(buf, LINE_SIZE, fp) == NULL) /* reed one line of file buf */
break; /* if fgets==NULL,it is the end of file,then break */
this_line_is_data = 1;
for (i = 0; buf[i]; i++) /* judge the content of the read line*/
{
if (!isdigit(buf[i]) && !isspace(buf[i]))
{ /* if there is any character of this line is not digit or space, ignore this line */
this_line_is_data = 0;
break;
}
}
if (!this_line_is_data)
continue; /* swith to next line */

ptr = buf;/* now is sequence line */
while (1)
{ /* read digits */
while (isspace(*ptr))
ptr++; /*ignore space*/
if (sscanf(ptr, "%d", data1+Length1) <= 0) /* if it is the end of this line read another line */
break;
Length1++;
while (isdigit(*ptr))
ptr++;
}
}
fclose(fp);/* close file */
printf("The length of first sequence is %d.\n\nAnd the sequence is\n", Length1);/* print lenght of sequence on screen */
for (i = 0; i < Length1; i++)/* print sequence */
printf("%d ", data1[i]);
printf("\n\n");

//Read the second sequence using the same technique as first sequence
printf("\nEnter second file path\n");
gets(Infile2);
fp = fopen(Infile2, "r");
if (fp == NULL)
{
printf("Failed to open file %s\n", Infile2);
return 1;
}

while (1)
{
if (fgets(buf, LINE_SIZE, fp) == NULL)
break;
this_line_is_data = 1;
for (i = 0; buf[i]; i++)
{
if (!isdigit(buf[i]) && !isspace(buf[i]))
{
this_line_is_data = 0;
break;
}
}
if (!this_line_is_data)
continue;


ptr = buf;
while (1)
{
while (isspace(*ptr))
ptr++;
if (sscanf(ptr, "%d", data2+Length2) <= 0)
break;
Length2++;
while (isdigit(*ptr))
ptr++;
}
}
fclose(fp);
printf("The length of second sequence is %d.\n\nAnd the sequence is\n", Length2);
for (i = 0; i < Length2; i++)
printf("%d ", data2[i]);
printf("\n\n");

//Porcess sequences
printf("Enter the degree of the sequences\n");/* set degree of sequence */
scanf("%d",degree);

//Put sequences into matrix
for(i = 0; i < Length2; i++)
{/* first sequence to column */
for(k = 0; k < Length1; k++)
{/* second sequence to row */
addresult = data1[k]+data2[i];/* adding digit of sequences */
if(addresult < degree)
matrix[i][k] = addresult;
else
matrix[i][k] = addresult-degree;
}
}

//Set first column as degree/2 or (degree-1)/2
for(i = 0; i < Length1; i++)
{
if(degree%2 == 0)
matrix[1][i] = degree/2;
else
matrix[1][i] = (degree-1)/2;
}
//Set first row as 0
for(i = 0; i < Length2; i++)
matrix[i][0] = 0;

//Use Euclid arithmetic to calculate gcd & lcm
m = Length1;
n = Length2;

if(n > m)/* Let m alway larger than n */
{
a = n;
n = m;
m = a;
}

if(m == n)/* calculate the gcd and lcm of length1 and length2 */
{
lcm = m;
gcd = m;
}
else
{
if(m%n == 0)
{
lcm = m;
gcd = n;
}
else
{
while(m%n == 0)/* gcd(m,n)=gcd(n,r) when r is the remain */
{
r = m%n;
m = n;
n = r;
}
gcd = n;
lcm = (Length1*Length2)/n;/* lcm=(m*n)/gcd */
}
}

//Get new sequences
column = 0;
row = 0;

for(i = 0; i < gcd; i++)
{/* the number of sequences is gcd */
for(k = 0; k < lcm; k++)
{/* the length of each sequence is lcm */
sequence[i][k] = matrix[column][row];/* get sequences slanting */
column++;
row++;
if(row = Length1)
row = 0;
if(column = Length2)
column = 0;
}
}
//Output sequences to files
printf("\nEnter the path to store new sequences without file name or \".txt\"\n");/* set output file path */
gets(Outfile);
for(i = 0; (Outfile[i] = tolower(Outfile[i])) != '/0'; i++);/* to lower the path namen */

while(strstr(Outfile, word) != NULL)/* search ".txt" in the file path, if it is exist, print fail and enter again */
{
printf("Wrong path!\n");
printf("\nEnter the path to store new sequences without file name or \".txt\"\n");/* set output file path again */
gets(Outfile);
for(i = 0; (Outfile[i] = tolower(Outfile[i])) != '/0'; i++);
}

while(Outfile[count] != '\0')/* count the number of letters of the path */
count++;

for(i=0; i < gcd; i++)
{/* output each sequence to one file */
temp = itoa(i+1, temp, 10);/* integer to array */
Outfile = strcat(Outfile, temp);/* copy the file number to the end of the path */
Outfile = strcat(Outfile, word);/* set the file as txt file */
fp = fopen(Outfile, "w");/* open file */
if (fp == NULL)
{
printf("Failed to open file %s\n", Infile1);
return 1;
}


fprintf(fp, "!New sequence is generated, this is the %d sequence and its degree = %d\n\n", i, degree);
for(k=0; k < lcm; k++)/* sequence write on the file */
fprintf(fp, "%d ", sequence[i][k]);
fprintf(fp, "\n\n!Length of this sequence is %d", lcm);
}

}
ChamPagneZ 2008-07-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jacksonsnake 的回复:]
itoa(i+1, temp, 10);/* integer to array */
[/Quote]
改成这样:

temp=itoa(i+1, temp, 10);
jacksonsnake 2008-07-21
  • 打赏
  • 举报
回复
不行啊,调试卡在itoa的地方
itoa(i+1, temp, 10);/* integer to array */
Outfile = strcat(Outfile, temp);/* copy the file number to the end of the path */
Outfile = strcat(Outfile, word);/* set the file as txt file */
jacksonsnake 2008-07-21
  • 打赏
  • 举报
回复
这样行不?
=======================================================
int sequence[32][256]={
{1,2,3,4,5,6,7,8,9},
{2,3,4,5,6,7,8,9,1},
{3,4,5,6,7,8,9,1,2)
};
char Outfile[20]="data";
char temp[10];
char word[]=".txt";
for(i=0; i < 3; i++)
{/* output each sequence to one file */
itoa(i+1, temp, 10);/* integer to array */
Outfile = strcat(Outfile, temp);
Outfile = strcat(Outfile, word);
fp = fopen(Outfile, "w")
if(fp == NULL)
printf("\nFail to write file %s", Outfile);
else
fprintf(fp, %s, sequence[i]);
fclose(fp);
}
jacksonsnake 2008-07-21
  • 打赏
  • 举报
回复
是比如说第一行写进一个新的文档文件“data 1.txt”,第二行写进“data 2.txt”,第i行写进“data i.txt”
ChamPagneZ 2008-07-21
  • 打赏
  • 举报
回复
是这种么?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char filename[256];
char *add=".txt";
FILE *fp=0;
int i=0;
char a[5][256]={
{"Hello"},
{","},
{"World"},
{"!"},
{"!!"}
};
for (i=0;i<5;i++){
char *temp=filename;
temp=itoa(i+1,temp,10);
temp=strcat(temp,add);
if((fp=fopen(temp,"w"))!=0)
fprintf(fp,"%s",a[i]);
}
return 0;

}

69,373

社区成员

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

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