Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文本文件中选择随机数据,并将其放在Excel电子表格中的特定位置_C# - Fatal编程技术网

C# 从文本文件中选择随机数据,并将其放在Excel电子表格中的特定位置

C# 从文本文件中选择随机数据,并将其放在Excel电子表格中的特定位置,c#,C#,我有一个问题:如何从一个文件中随机选择一段数据并将其放入.csv文件中?我知道您需要设置路径,以便在计算机中定位文件,但我想知道何时使用random方法或另一种使用random的方法,因为在C中,random主要用于整数和双精度 例如:我在一个文本文件中有一个名称列表: Kenneth Samuel Samantha Catherine Danielle Jonathan Ellen Valentin Christopher Edward 现在,我想将其中一个名称放在.CSV文件中的特定位置。

我有一个问题:如何从一个文件中随机选择一段数据并将其放入.csv文件中?我知道您需要设置路径,以便在计算机中定位文件,但我想知道何时使用random方法或另一种使用random的方法,因为在C中,random主要用于整数和双精度

例如:我在一个文本文件中有一个名称列表:

Kenneth
Samuel
Samantha
Catherine
Danielle
Jonathan
Ellen
Valentin
Christopher
Edward
现在,我想将其中一个名称放在.CSV文件中的特定位置。以下是我到目前为止所做的尝试:

namespace ConsoleApp
{
    class Program
    {

        public static List<RecordStructure> CSVRecords = new List<RecordStructure>();

        static void Main(string[] args)
        {
            Console.WriteLine("Data Mask Process");
            Console.WriteLine("**************************");

            CSVRecords.AddRange(ReadCSVFile());

            // some things I wrote for a work :)
            string contentF_names = File.ReadAllText(@"C:\\path");
            string contentM_names = File.ReadAllText(@"C:\\path");
            string contentNames = File.ReadAllText(@"C:\\path");
            string contentPlaces = File.ReadAllText(@"C\\path");

            // For the random part
            Random r = new Random();

            var line = contentF_names[r.Next(contentF_names.Length)];

            // Printing and ending

            Console.WriteLine(CSVRecords[1].firstname);
            Console.WriteLine(CSVRecords[1].lastname);
            Console.WriteLine(CSVRecords[1].city);

            Console.WriteLine("**************************");
            Console.WriteLine("End of Process.");
            Console.ReadKey();
        }

        public static List<RecordStructure> ReadCSVFile()
        {
            List<RecordStructure> RecordList = new List<RecordStructure>();

            StreamReader reader = new StreamReader(File.OpenRead(@"path"));

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();

                if (!String.IsNullOrWhiteSpace(line))
                {
                    string[] values = line.Split(',');

                    RecordList.Add(new RecordStructure
                                       {
                                           firstname = values[0],
                                           lastname = values[1],
                                           company_name = values[2],
                                           city = values[3]
                                       });
            }
        }

        return RecordList;
    }
}

class RecordStructure
{
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string company_name { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public string state { get; set; }
    public string zip { get; set; }
    public string phone1 { get; set; }
    public string phone2 { get; set; }
    public string email { get; set; }
    public string web { get; set; }
}
因为您正在处理CVS文件,所以可以将结果转换为数组或列表,然后将其索引与随机索引一起使用以获得随机索引

例如:

var contentF_names = "Kenneth,Samuel,Samantha,Catherine,Danielle,Jonathan,Ellen,Valentin,Christopher,Edward";

// ToList
var contentF_namesArray = contentF_names.Split(',').ToList();

//For the random part

Random r = new Random();

// Get Random index between 0 and the total number of elements in the list
var randomIndex = r.Next(0, contentF_namesArray.Count);

// Use the random index to get an element from the list.
var randomFirstName = contentF_namesArray[randomIndex];
/*
    Get Five Random Names 
*/

for (int x =0; x <= 5; x++)
{
    // Get Random index between 0 and the total number of elements in the list
    var randomIndex = r.Next(0, contentF_namesArray.Count);

    // Use the random index to get an element from the list.
    var randomFirstName = contentF_namesArray[randomIndex];

    Console.WriteLine(randomFirstName);
}
randomFirstName将保存随机名称,并且每次运行时。您可以使用循环来获得x个随机名称

例如:

var contentF_names = "Kenneth,Samuel,Samantha,Catherine,Danielle,Jonathan,Ellen,Valentin,Christopher,Edward";

// ToList
var contentF_namesArray = contentF_names.Split(',').ToList();

//For the random part

Random r = new Random();

// Get Random index between 0 and the total number of elements in the list
var randomIndex = r.Next(0, contentF_namesArray.Count);

// Use the random index to get an element from the list.
var randomFirstName = contentF_namesArray[randomIndex];
/*
    Get Five Random Names 
*/

for (int x =0; x <= 5; x++)
{
    // Get Random index between 0 and the total number of elements in the list
    var randomIndex = r.Next(0, contentF_namesArray.Count);

    // Use the random index to get an element from the list.
    var randomFirstName = contentF_namesArray[randomIndex];

    Console.WriteLine(randomFirstName);
}

非常感谢你的回答。但不幸的是,它给了我一个零。在随机部分,我使用了这个方法:Convert.toString内容f_names[r.NextcontentF_names.Length]//它返回names@AllenDario糟糕的是,我忘了包括最后一部分。我已经用一些解释更新了代码。谢谢你的代码,我很感激。我有一些错误,但那很好,因为我知道发生了什么,我能处理。非常感谢。