有c代码求换成c#代码

xjgcyq1 2017-06-01 06:57:13
#include <stdio.h>
#include <stdlib.h>
int main()
{
int g[1000];//开一个足够大的数组。
int i99 = 0;
int na;
FILE *fp;//文件指针
fp = fopen("d:\\a.txt", "r");//以文本方式打开文件。
if(fp == NULL) //打开文件出错。
return -1;
while(fscanf(fp, "%d,", &g[i99]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
i99++;
fclose(fp);//关闭文件
for( na= 0;na < i99; na ++)//循环输出数组元素。
{
printf("%d ", g[na]);
}
printf("\n");
printf("na=%d\n ", na);
return 0;
}

a.txt
16,12,13,15,14,
17,12,12,13,14,
18,12,13,1,1,
...全文
145 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2017-06-02
  • 打赏
  • 举报
回复
            var g = new int[1000];
            int i99 = 0;
            foreach (var r in File.ReadAllLines("a.txt"))
            {
                foreach (var x in r.Split(','))
                {
                    g[i99++] = int.Parse(x);
                }
            }
还可以这样
            var s = File.ReadAllText("a.txt");
            var a = Regex.Matches(s, @"(?s)\d+").Cast<Match>().Select(x => int.Parse(x.Value)).ToArray();
xjgcyq1 2017-06-02
  • 打赏
  • 举报
回复
万分感谢!怎样把数存入int数组,开一个int数组,把这些数都存入数组,int.Parse(str1);只能单个存。
hljhyb 2017-06-01
  • 打赏
  • 举报
回复
using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] Byte;
            using (FileStream file =new FileStream("d:\\a.txt", FileMode.Open))
            {
                int len = (int)file.Length;
                Byte = new byte[len];
                file.Read(Byte, 0, Byte.Length);
            }
            string str = System.Text.Encoding.UTF8.GetString(Byte);
            //str= str.Replace("\r\n","");//如果不换行就取消这个批注
            int i;
            for (i=0; str.IndexOf(",") > 0;i++)
            {
                string str1 = str.Substring(0, str.IndexOf(","));
                Console.Write(str1+" ");
                str = str.Substring(str.IndexOf(",") + 1);
            }
            Console.Write("\r\n");
            Console.Write(i.ToString());
            Console.ReadKey();
        }
    }
}
额 我又整理了下代码
hljhyb 2017-06-01
  • 打赏
  • 举报
回复
using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] Byte;
            using (FileStream file =new FileStream("d:\\a.txt", FileMode.Open))
            {
                int len = (int)file.Length;
                Byte = new byte[len];
                file.Read(Byte, 0, Byte.Length);
            }
            string str = System.Text.Encoding.UTF8.GetString(Byte);
            //str= str.Replace("\r\n","");//如果不换行就取消这个批注
            int[] g = new int[1000];
            for (int i=0; str.IndexOf(",") > 0;i++)
            {
                string str1 = str.Substring(0, str.IndexOf(","));
                Console.Write(str1+" ");
                str = str.Substring(str.IndexOf(",") + 1);
            }
            
            
            Console.ReadKey();
        }
    }
}
这个是你要的意思吗
xjgcyq1 2017-06-01
  • 打赏
  • 举报
回复
不是作业也不是练习题,菜鸟学编程。
ilikeff8 2017-06-01
  • 打赏
  • 举报
回复
这是作业还是面试题

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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