有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,
...全文
165 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
  • 打赏
  • 举报
回复
这是作业还是面试题
一个很强大的工具, 能将c#代码片段、文件甚至工程直接转换成java代码,并能彻底解决外部引用的DLL问题,最强的是支持c#工程的直接转换,生成的Java代码质量也很不错。软件已破解,去除了未注册版最多只能转换1000行的限制,亲测可用!压缩包内含帮助文档,不过由于软件的使用很简单,帮助文档基本可以忽略。(小提示:如无法运行,请确认是否安装了.NET framework) 下面是一些英文介绍: C# to Java Converter features include: 1. Folder conversion: Entire folders of C# code files are converted to Java code files.(文件夹转换) 2. Code snippet and file conversion: Our snippet conversion accuracy is outstanding and does not require you to insert entire methods or classes. Heuristics are used to convert code fragments wit h missing declarations. (代码片段和文件转换) 3. Full support for the latest .NET code syntax: Generics and other newer C# features are converted where possible to the Java equivalent.(全面支持最新版的.NET语法) 4. Conversion options: You can specify numerous conversion options via our Options dialog. Code formatting options, custom type & member replacements, custom string replacements, and miscellaneous options. (可以指定转换规则) 5. File comparison feature: After converting a project or folder, you can compare original and converted files side-by-side. Comparing converted code to original code.(原代码与转换后代码的比较) 6. Running from the command line: C# to Java Converter can be launched directly for a specific project, folder, or file conversion from the command line. Command line.(命令行执行) 其他一些特点: 1. Converts all versions of C# code (2002, 2003, 2005, 2008, and 2010) (可以转换所有版本的C#代码) 2. Evaluates all referenced assemblies and .NET projects in order to resolve external references more completely.(能彻底解决外部引用的dll类库) 3. Converts C# ref parameters using Java generics 4. Superb conversions of all types of arrays 5. Handles the numerous coding alternatives and ambiguities of C# code 6. Flawless conversion of all aspects of inheritance and interfaces 7. Allows custom replacement of strings in the final converted code 8. Accurate even with poorly formatted C# code

111,120

社区成员

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

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

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