web-dev-qa-db-fra.com

Comment lire un fichier et écrire dans un fichier texte?

Je veux ouvrir un fichier, copier toutes les données et écrire dans un fichier texte.

Mon mauvais dossier.

Nom du fichier - 1.mis

M3;3395;44;0;1;;20090404;094144;8193;3;0;;;;
M3;3397;155;0;2;;20090404;105941;8193;3;0;;;;
M3;3396;160;0;1;;20090404;100825;8193;3;0;;;;
M3;3398;168;0;2;;20090404;110106;8193;3;0;;;;

bientôt...,

Les données ci-dessus doivent apparaître dans un fichier texte avec le même nom de fichier (1.txt).

J'ai essayé ce code.

Dim sFileText As String
Dim iFileNo As Integer
iFileNo = FreeFile
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo
Do While Not EOF(iFileNo)
Input #iFileNo, sFileText
Loop
Close #iFileNo

Open "C:\Clients\Converter\2.txt" For Output As #iFileNo
Do While Not EOF(iFileNo)
Write #iFileNo, sFileText
Loop
Close #iFileNo

Rien n'est enregistré dans 1.txt.

9
Gopal

Il est beaucoup plus facile d'utiliser le runtime de script installé par défaut sur Windows

Allez simplement sur Référence du projet et vérifiez Microsoft Scripting Runtime et cliquez sur OK.

Ensuite, vous pouvez utiliser ce code qui est bien meilleur que les commandes de fichiers par défaut

Dim FSO As FileSystemObject
Dim TS As TextStream
Dim TempS As String
Dim Final As String
Set FSO = New FileSystemObject
Set TS = FSO.OpenTextFile("C:\Clients\Converter\Clockings.mis", ForReading)
'Use this for reading everything in one shot
Final = TS.ReadAll
'OR use this if you need to process each line
Do Until TS.AtEndOfStream
    TempS = TS.ReadLine
    Final = Final & TempS & vbCrLf
Loop
TS.Close

Set TS = FSO.OpenTextFile("C:\Clients\Converter\2.txt", ForWriting, True)
    TS.Write Final
TS.Close
Set TS = Nothing
Set FSO = Nothing

Quant à ce qui ne va pas avec votre code d'origine ici, vous lisez chaque ligne du fichier texte.

Input #iFileNo, sFileText

Ensuite, vous l'écrivez ici

Write #iFileNo, sFileText

sFileText est une variable de chaîne, donc ce qui se passe, c'est que chaque fois que vous lisez, vous remplacez simplement le contenu de sFileText par le contenu de la ligne que vous venez de lire.

Donc, quand vous allez l'écrire, tout ce que vous écrivez est la dernière ligne que vous lisez, qui est probablement une ligne vierge.

Dim sFileText As String
Dim sFinal as String
Dim iFileNo As Integer
iFileNo = FreeFile
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo
Do While Not EOF(iFileNo)
  Input #iFileNo, sFileText
sFinal = sFinal & sFileText & vbCRLF
Loop
Close #iFileNo

iFileNo = FreeFile 'Don't assume the last file number is free to use
Open "C:\Clients\Converter\2.txt" For Output As #iFileNo
Write #iFileNo, sFinal
Close #iFileNo

Notez que vous n'avez pas besoin de faire une boucle pour écrire. sFinal contient le texte complet du fichier prêt à être écrit en une seule fois. Notez que l'entrée lit une LIGNE à la fois, donc chaque ligne ajoutée à sFinal doit avoir un CR et LF ajouté à la fin pour être correctement écrit sur un système MS Windows. Un autre système d'exploitation peut juste besoin d'un LF (Chr $ (10)).

Si vous devez traiter les données entrantes, vous devez faire quelque chose comme ça.

Dim sFileText As String
Dim sFinal as String
Dim vTemp as Variant
Dim iFileNo As Integer
Dim C as Collection
Dim R as Collection
Dim I as Long
Set C = New Collection
Set R = New Collection

iFileNo = FreeFile
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo
Do While Not EOF(iFileNo)
  Input #iFileNo, sFileText
  C.Add sFileText
Loop
Close #iFileNo

For Each vTemp in C
     Process vTemp
Next sTemp

iFileNo = FreeFile
Open "C:\Clients\Converter\2.txt" For Output As #iFileNo
For Each vTemp in R
   Write #iFileNo, vTemp & vbCRLF
Next sTemp
Close #iFileNo
15
RS Conley

Si vous voulez le faire ligne par ligne:

Dim sFileText As String
Dim iInputFile As Integer, iOutputFile as integer

iInputFile = FreeFile
Open "C:\Clients\Converter\Clockings.mis" For Input As #iInputFile 
iOutputFile = FreeFile
Open "C:\Clients\Converter\2.txt" For Output As #iOutputFile 
Do While Not EOF(iInputFile)
   Line Input #iInputFile , sFileText
   ' sFileTextis a single line of the original file
   ' you can append anything to it before writing to the other file
   Print #iOutputFile, sFileText 
Loop
Close #iInputFile 
Close #iOutputFile 
4
C-Pound Guru
FileCopy "1.mis", "1.txt"
2
nothrow
    An example of reading a file:
Dim sFileText as String
Dim iFileNo as Integer
iFileNo = FreeFile
'open the file for reading
Open "C:\Test.txt" For Input As #iFileNo
'change this filename to an existing file! (or run the example below first)

'read the file until we reach the end
Do While Not EOF(iFileNo)
Input #iFileNo, sFileText
'show the text (you will probably want to replace this line as appropriate to your program!)
MsgBox sFileText
Loop

'close the file (if you dont do this, you wont be able to open it again!)
Close #iFileNo
(note: an alternative to Input # is Line Input # , which reads whole lines).


An example of writing a file:
Dim sFileText as String
Dim iFileNo as Integer
iFileNo = FreeFile
'open the file for writing
Open "C:\Test.txt" For Output As #iFileNo
'please note, if this file already exists it will be overwritten!

'write some example text to the file
Print #iFileNo, "first line of text"
Print #iFileNo, " second line of text"
Print #iFileNo, "" 'blank line
Print #iFileNo, "some more text!"

'close the file (if you dont do this, you wont be able to open it again!)
Close #iFileNo

De ici

2
Khodor