IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Framework .NET Discussion :

lire un fichier texte et separer les colonnes


Sujet :

Framework .NET

  1. #1
    Nouveau Candidat au Club
    Inscrit en
    Décembre 2006
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 3
    Points : 1
    Points
    1
    Par défaut lire un fichier texte et separer les colonnes
    Bonjour

    Je suis tout nouveau et j'aurai besoin d'un peu d'aide
    mon probleme est le suivant

    j'ai un fichier texte
    ce fichier texte se presente sous la forme suivante
    colonne A Colonne B colonne C colonne D
    et cela sur plusieurs ligne
    chaque colonne est separer par une tabulation
    sauf la colonne C et D. La colonne D commence par " et se termine par "
    le probleme de cette colonne est que chaque caractere (y compris les spaces doivent etre stockes dans un tableau.
    pour les valeurs des colonnes A , B et C celle ci sont stockes avec des int.
    Merci d'avance

  2. #2
    Nouveau Candidat au Club
    Inscrit en
    Décembre 2006
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    voici ce que j ai commence a faire (avec beaucoup d'aide )
    j'aimerais donc copier le string left dans mon tableau de byte tab
    est ce qu'un commande permet cela ?
    merci d'avance


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.InteropServices;
     
     
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            List<string> GetList(string text)
            {
                List<string> Columns = new List<string>();
                int index = text.IndexOf('\t');
                while (index != -1)
                {
                    string s = text.Substring(0, index);
                    Columns.Add(s);
                    if (index == text.Length - 1)
                        break;
                    text = text.Substring(index + 1);
                }
                return Columns;
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                StreamReader reader = new StreamReader(new FileStream(@"c:\temp\file.txt", FileMode.Open, FileAccess.Read));
     
                while (!reader.EndOfStream)
                {
                    List<string> Columns = new List<string>();
                    string Line = reader.ReadLine();
                    byte[] tab;
                    string RemaingChars = Line;
                    int i=0;
                    while (RemaingChars.Length != 0)
                    {
                        int indexQuot = RemaingChars.IndexOf('\t');
                        if (indexQuot == -1)
                        {
                            Columns.AddRange(GetList(RemaingChars));
                            break;
                        }
                        string left = RemaingChars.Substring(0, indexQuot);
     
     
                        //tab est un tableau de byte et left un string le probleme est la 
                        //tab[i] = (byte)ConvertTo(left, typeof(byte));
                        i++;
     
                        string right = "";
                        if (indexQuot < RemaingChars.Length - 1)
                            right = RemaingChars.Substring(indexQuot + 1);
                        Columns.AddRange(GetList(left));
                        indexQuot = right.IndexOf('\t');
                        if (indexQuot == -1)
                            Columns.Add(right);
                        else if (indexQuot < right.Length - -1)
                            Columns.Add(right.Substring(0, indexQuot));
                        RemaingChars = right.Substring(indexQuot + 1);
                    }
                }
            }
        }
    }

  3. #3
    Nouveau Candidat au Club
    Inscrit en
    Décembre 2006
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.InteropServices;
     
     
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                StreamReader reader = new StreamReader(new FileStream(@"c:\temp\file.txt", FileMode.Open, FileAccess.Read));
     
                while (!reader.EndOfStream)
                {
                    string Line = reader.ReadLine();
                    string RemaingChars = Line;
                    int nombrecolonne = 0;
                    int nombrecol = 0;
                    int toto = 0;
     
                    while (RemaingChars.Length != 0 && toto!=2)
                    {
                        int indexQuot = RemaingChars.IndexOf('\t');
                        while (toto != 1)//boucle qui permet de connaitre le nombre de colonnes
                        {
                            indexQuot = RemaingChars.IndexOf('\t') ;
                            if (indexQuot == -1)
                            {
                                indexQuot--;
                            }
                            if (indexQuot <= -2)
                            {
                                toto = 1;
                                break;
                            }
     
     
                            if (indexQuot >= 0)
                            {
                                string left = RemaingChars.Substring(0, indexQuot);
                                nombrecol++;
                            }
                            RemaingChars = RemaingChars.Substring(indexQuot + 1);
                        }
                        nombrecolonne = nombrecol + 1;
                        nombrecol = 0;
                        string[] tab = new string[nombrecolonne];
                        RemaingChars = Line;
                        while (toto != 2)//boucle qui ecrit dans un tableau de string les valeurs de la ligne
                        {
                            indexQuot = RemaingChars.IndexOf('\t');
                            if (indexQuot == -1)
                            {
                                tab[nombrecolonne-1] = RemaingChars;
                                indexQuot--;
                            }
                            if (indexQuot <= -2)
                            {
                                toto = 2;
                                break;
                            }
     
     
                            if (indexQuot >= 0)
                            {
                                string left = RemaingChars.Substring(0, indexQuot);
                                tab[nombrecol] = left;
                                nombrecol++;
                            }
                            RemaingChars = RemaingChars.Substring(indexQuot + 1);
     
                          }
     
     
                    }
                }
            }
        }
    }
    voila ou j en suis pour l instant un tableau de string qui recopie des valeurs separer par des tabulations

Discussions similaires

  1. Réponses: 2
    Dernier message: 02/03/2012, 12h09
  2. Lire un fichier texte avec colonnes de longueur variable
    Par Jack_nicholson dans le forum SAS Base
    Réponses: 5
    Dernier message: 07/03/2011, 15h03
  3. Réponses: 5
    Dernier message: 21/02/2007, 16h12
  4. Réponses: 20
    Dernier message: 23/03/2006, 16h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo