在用C语言中,如何将一个二维数组存储到文本文件中?
我有一个4×5的二维数组,数组名是array;数组内容为:12234247905678358621;我要怎么写代码,将这个数组数据存储到磁盘文件上去呢?是用fwrite吗...
我有一个4×5的二维数组,数组名是array;
数组内容为:
1 2 2 3 4
2 4 7 9 0
5 6 7 8 3
5 8 6 2 1;
我要怎么写代码,将这个数组数据存储到磁盘文件上去呢?是用fwrite吗? 展开
数组内容为:
1 2 2 3 4
2 4 7 9 0
5 6 7 8 3
5 8 6 2 1;
我要怎么写代码,将这个数组数据存储到磁盘文件上去呢?是用fwrite吗? 展开
4个回答
展开全部
fwrite 用于写 2 进制文件。用 fprintf 就可以了。
int a[4][5]={1 ,2,2,3,4,2,4,7,9,0,5,6,7,8,3,5,8,6,2,1};
FILE *fout;
int i,j;
fout = fopen("abc.txt","w"); // 打开文件,文本输出文件
for (j=0;j<4;j++){
for (i=0;i<5;i++){ fprintf(fout,"%d ",a[j][i]);} // 写
fprintf(fout,"\n");
}
fclose(fout); //关闭
int a[4][5]={1 ,2,2,3,4,2,4,7,9,0,5,6,7,8,3,5,8,6,2,1};
FILE *fout;
int i,j;
fout = fopen("abc.txt","w"); // 打开文件,文本输出文件
for (j=0;j<4;j++){
for (i=0;i<5;i++){ fprintf(fout,"%d ",a[j][i]);} // 写
fprintf(fout,"\n");
}
fclose(fout); //关闭
追问
请问下,如果用子函数的形式写该怎么写呢?就是说数组数据在主函数中,存储代码为一个子函数~
追答
FILE *fout;
void fun(){
int i,j;
int a[4][5]={1 ,2,2,3,4,2,4,7,9,0,5,6,7,8,3,5,8,6,2,1};
for (j=0;j<4;j++){
for (i=0;i<5;i++){ fprintf(fout,"%d ",a[j][i]);} // 写
fprintf(fout,"\n");
}
}
main()
{
int i,j;
fout = fopen("abc.txt","w"); // 打开文件,文本输出文件
fun(); // 写
fclose(fout); //关闭
}
===
你愿意用void fun( FILE *fout); 传递FILE 也可以
展开全部
完整代码:
#include <cstdio>
int array[4][5];
int main(){
freopen("a.txt","w",stdout);
for(int i=0;i<4;i++){
for(int j=0;j<5;j++){
printf("%d ",array[i][j]);
}
printf("\n");
}
return 0;
}
#include <cstdio>
int array[4][5];
int main(){
freopen("a.txt","w",stdout);
for(int i=0;i<4;i++){
for(int j=0;j<5;j++){
printf("%d ",array[i][j]);
}
printf("\n");
}
return 0;
}
更多追问追答
追问
你好,你的程序好似对的~可是我将你写的程序作为子函数,在主函数中新建得到一个数组,将这个数组传参到子函数中,为啥一这样做,你的程序就不行了呢?就不能将主函数中的数组存档了呢?
追答
能把代码贴上来吗?
我看看
是不是参数传递有问题?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
FILE *fp;
fp=open("my.txt","w");
if(fp!=NULL)
{
for(i=0;i<4;i++)
for(j=0;j<5;j++)
fprintf(fp,"%d",array[i]);
}
fp=open("my.txt","w");
if(fp!=NULL)
{
for(i=0;i<4;i++)
for(j=0;j<5;j++)
fprintf(fp,"%d",array[i]);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以这样:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int line = 0;
string strLine="";
FileStream fs;
OpenFileDialog openf = new OpenFileDialog();
openf.InitialDirectory = "c:\\";
openf.Filter = "exe files (*.txt)|*.txt";
openf.FilterIndex = 2;
openf.RestoreDirectory = true;
if (openf.ShowDialog() == DialogResult.OK)
{
try
{
fs = new FileStream(openf.FileName, FileMode.Open);
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
do
{
strLine = m_streamReader.ReadLine();
line++;
} while (strLine != null && strLine.Length > 0);
string[,] array = new string[line, 4]; // 声明输出数组
m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
line = 0;
do
{
strLine = m_streamReader.ReadLine();
if (strLine!=null && strLine.Contains('|'))
{
string[] spli = strLine.Split('|');
array[line, 0] = spli[0];
array[line, 1] = spli[1];
array[line, 2] = spli[2];
array[line, 3] = spli[3];
line++;
}
} while (strLine != null && strLine.Length > 0);
string temp = "";
for (int i = 0; i < line; i++)
{
temp = temp + array[i, 0];
temp = temp + "|" + array[i, 1];
temp = temp + "|" + array[i, 2];
temp = temp + "|" + array[i, 3] + "\n";
}
MessageBox.Show(temp);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int line = 0;
string strLine="";
FileStream fs;
OpenFileDialog openf = new OpenFileDialog();
openf.InitialDirectory = "c:\\";
openf.Filter = "exe files (*.txt)|*.txt";
openf.FilterIndex = 2;
openf.RestoreDirectory = true;
if (openf.ShowDialog() == DialogResult.OK)
{
try
{
fs = new FileStream(openf.FileName, FileMode.Open);
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
do
{
strLine = m_streamReader.ReadLine();
line++;
} while (strLine != null && strLine.Length > 0);
string[,] array = new string[line, 4]; // 声明输出数组
m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
line = 0;
do
{
strLine = m_streamReader.ReadLine();
if (strLine!=null && strLine.Contains('|'))
{
string[] spli = strLine.Split('|');
array[line, 0] = spli[0];
array[line, 1] = spli[1];
array[line, 2] = spli[2];
array[line, 3] = spli[3];
line++;
}
} while (strLine != null && strLine.Length > 0);
string temp = "";
for (int i = 0; i < line; i++)
{
temp = temp + array[i, 0];
temp = temp + "|" + array[i, 1];
temp = temp + "|" + array[i, 2];
temp = temp + "|" + array[i, 3] + "\n";
}
MessageBox.Show(temp);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询