Integrer chak ligne de fichier ds une case d'un tableau
cs_titima
Messages postés2Date d'inscriptionmardi 21 juin 2005StatutMembreDernière intervention22 août 2005
-
22 août 2005 à 16:14
Lutinore
Messages postés3246Date d'inscriptionlundi 25 avril 2005StatutMembreDernière intervention27 octobre 2012
-
23 août 2005 à 17:44
bijour
je travaille sur les fichiers ,et du a une comparaison de deux fichier g pensé au principe des tableaux ,donc g consideré une ligne d'un fichier comme une case de tableau mais mon programme se bloque sur la premiere ligne ..voici mon code ,svp dites moi pourkoi il considaire le fichier comme suivant:
g un ficihier txt :
aaaaaaa
bbbbbb
cccccc et qd je mets tab[i]=sr.ReadLine();
il le voit comme suit :
tab[0]=aaaaaaa
tab[1]=""
tab[2]=""
et qd g mis tab1[i]=sr1.ReadToEnd();
g eu ca :
tab[0]=aaaaaaa bbbbbbb ccccccc
tab[1]=""
.
.
mon code est le voici
StreamReader sr1 = File.OpenText(FILE_NAME1);
StreamReader sr2 = File.OpenText(FILE_NAME2);
string [] tab1=
new
string[FILE_NAME1.Length];
string [] tab2=
new
string[FILE_NAME2.Length];
for(
int i=0;i<FILE_NAME1.Length;i++)
{
for(
int j=0;j<FILE_NAME2.Length;j++)
{
tab1[i]=sr1.ReadLine();
tab2[i]=sr2.ReadLine();
if(tab2[i+j]!=tab1[i])
{
if(tab1[i]==tab2[i+j+1])
{
FileStream fichier1=
new FileStream("C:\\modifier1.txt",FileMode.Create,FileAccess.Write);
StreamWriter sw1 =
new StreamWriter(fichier1);
sw1.WriteLine(tab2[i+j].ToString());
sw1.Close();
fichier1.Close();
}
else
{
MessageBox.Show("aucune ligne n'est modifiée");
j++;
}
if(tab1[i+j+1]==tab2[i])
{
FileStream fichier2=
new FileStream("C:\\desinstaller.txt",FileMode.Create,FileAccess.Write);
StreamWriter sw2 =
new StreamWriter(fichier2);
sw2.WriteLine(tab1[i+j].ToString());
sw2.Close();
fichier2.Close();
}
else
{
MessageBox.Show("aucune ligne n'est desinstallée");
j++;
}
}
else
{
j++;
}
}
brek;
}
}
A voir également:
Integrer chak ligne de fichier ds une case d'un tableau
Lutinore
Messages postés3246Date d'inscriptionlundi 25 avril 2005StatutMembreDernière intervention27 octobre 201241 23 août 2005 à 01:09
Salut, c'est vrai que c'est pas très clair, petit rappel ReadLine lit une ligne et ReadToEnd lit jusqu'a la fin du fichier. Tu peux utiliser un ArrayList:
StreamReader sr = null;
try
{
string line = string.Empty; ArrayList list new ArrayList( ); // Capacity 16
sr = new StreamReader( FILE_NAME );
while ( ( line = sr.ReadLine( ) ) != null )
list.Add( line );