Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COBOL ERROR : Transfering data from txt file to another 1

Status
Not open for further replies.

AMCM

Programmer
Feb 22, 2023
18
0
0
MR
HELLO ,

IDENTIFICATION DIVISION.
PROGRAM-ID. SeqRead.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO "C:\Users\Hp\Documents\NT5.txt"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT NSTUDENT ASSIGN TO "C:\Users\Hp\Documents\FILE-2.txt"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
88 EndOfStudentFile VALUE HIGH-VALUES.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(10).
02 YOBirth PIC 9(8).

02 CourseCode PIC X(4).
02 gender pic x(1) .
02 malecount pic 9(3) value zeros.
02 femalecount pic 9(3) value zeros.
FD NSTUDENT.
01 Ndetail.
02 NId PIC 9(7).
02 NName.
03 NSurname PIC X(10).
02 NYOBirth PIC 9(8).

02 NCourseCode PIC X(4).
02 Ngender pic x(1) .
02 Nmalecount pic 9(3) value zeros.
02 Nfemalecount pic 9(3) value zeros.
PROCEDURE DIVISION.
Begin.
OPEN INPUT StudentFile
OPEN OUTPUT NSTUDENT
PERFORM until EndOfStudentFile
READ StudentFile
AT END SET EndOfStudentFile TO TRUE
MOVE StudentDetails to Ndetail
DISPLAY Ndetail
WRITE Ndetail
END-PERFORM
CLose NSTUDENT
CLose StudentFile.
STOP RUN.

Im learning cobol ( still a beginner) , i have written the above code trying to transfer data from a .txt file to another but nothing tranfers and i get one letter written 36 times on the new file .
thise is the result file : ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
The originzl file is 36 lines like this one : 8712351SMITH MS19671012LM51F.
There is no error showing before compiling .


 
What COBOL compiler are you using ?

IMO the file record description is not correct
You have
Code:
       FD StudentFile.
          01 StudentDetails.
             88 EndOfStudentFile VALUE HIGH-VALUES.
             02 StudentId PIC 9(7).
             02 StudentName.
                03 Surname PIC X(10).
             02 YOBirth PIC 9(8).

       FD NSTUDENT.
          01 Ndetail.
             02 NId PIC 9(7).
             02 NName.
                03 NSurname PIC X(10).
             02 NYOBirth PIC 9(8).
You say that the line looks like
[pre]8712351SMITH MS19671012LM51F[/pre]
i.e.:
8712351 - this is StudentId or NId
SMITH MS - this shoud be Surname
19671012 - this should be YOBirth or NYOBirth
LM51F - what is this ?

And why you placed the 88-level field EndOfStudentFile into the record 01 StudentDetails ?
 
Thank for responding .
IM using Opencobol IDE
LM51 IS THE COURSE CODE and the F is the gender .
I Placed the boolean there in order not to use a W-S section , Sometimes i get errors whithout explanation the ide just crashes .
DO you think if i put the whole line in an X variable that has enough space for them all , will that be enough ? I just used a txt file i had from another program that displays student information .
 
You have to define record properly, or as you wrote, you can put the whole line in one field.
 
Hello AMCM,

Here I have written a short example program, which copies the text file
[tt]c:\tmp\cobol\my_input.txt[/tt]
line by line into the text file
[tt]c:\tmp\cobol\my_output.txt[/tt]

I have tested it in OpenCobolIDE

CPYFILES.COB
Code:
       [COLOR=#804040][b]IDENTIFICATION[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]PROGRAM-ID[/b][/color].  CPYFILES.
         [COLOR=#804040][b]AUTHOR[/b][/color].  MIKROM.

       [COLOR=#804040][b]ENVIRONMENT[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]INPUT-OUTPUT[/b][/color][COLOR=#804040][b] SECTION[/b][/color].
       [COLOR=#804040][b]FILE-CONTROL[/b][/color].
       [COLOR=#804040][b]SELECT[/b][/color] MY-INPUT-FILE [COLOR=#804040][b]ASSIGN[/b][/color] [COLOR=#804040][b]TO[/b][/color] [COLOR=#ff00ff]"c:\tmp\cobol\my_input.txt"[/color]
      [COLOR=#6a5acd]         [/color][COLOR=#804040][b]ORGANIZATION[/b][/color] [COLOR=#804040][b]IS[/b][/color] LINE [COLOR=#804040][b]SEQUENTIAL[/b][/color].
       [COLOR=#804040][b]SELECT[/b][/color] MY-OUTPUT-FILE [COLOR=#804040][b]ASSIGN[/b][/color] [COLOR=#804040][b]TO[/b][/color] [COLOR=#ff00ff]"c:\tmp\cobol\my_output.txt"[/color]
      [COLOR=#6a5acd]         [/color][COLOR=#804040][b]ORGANIZATION[/b][/color] [COLOR=#804040][b]IS[/b][/color] LINE [COLOR=#804040][b]SEQUENTIAL[/b][/color].

       [COLOR=#804040][b]DATA[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]FILE[/b][/color][COLOR=#804040][b] SECTION[/b][/color].
       [COLOR=#804040][b]FD[/b][/color] MY-INPUT-FILE.
       [COLOR=#2e8b57][b]01 [/b][/color]MY-INPUT-LINE    [COLOR=#804040][b]PIC[/b][/color] X([COLOR=#ff00ff]80[/color]).
       [COLOR=#804040][b]FD[/b][/color] MY-OUTPUT-FILE.
       [COLOR=#2e8b57][b]01 [/b][/color]MY-OUTPUT-LINE   [COLOR=#804040][b]PIC[/b][/color] X([COLOR=#ff00ff]80[/color]).
       [COLOR=#804040][b]WORKING-STORAGE[/b][/color][COLOR=#804040][b] SECTION[/b][/color].
       [COLOR=#2e8b57][b]01 [/b][/color]END-OF-FILE [COLOR=#804040][b]PIC[/b][/color] X [COLOR=#804040][b]VALUE[/b][/color] [COLOR=#ff00ff]SPACE[/color].
          [COLOR=#6a5acd]88 [/color]END-OF-INPUT-FILE [COLOR=#804040][b]VALUE[/b][/color] [COLOR=#ff00ff]'T'[/color].
          [COLOR=#6a5acd]88 [/color]NOT-END-OF-INPUT-FILE [COLOR=#804040][b]VALUE[/b][/color] [COLOR=#ff00ff]'F'[/color].

       [COLOR=#804040][b]PROCEDURE[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]MAIN-PARA[/b][/color].
         [COLOR=#804040][b]OPEN[/b][/color] [COLOR=#804040][b]INPUT[/b][/color] MY-INPUT-FILE
         [COLOR=#804040][b]OPEN[/b][/color] [COLOR=#804040][b]OUTPUT[/b][/color] MY-OUTPUT-FILE

         [COLOR=#804040][b]SET[/b][/color] NOT-END-OF-INPUT-FILE [COLOR=#804040][b]TO[/b][/color] [COLOR=#804040][b]TRUE[/b][/color]

         [COLOR=#008080]PERFORM[/color] [COLOR=#804040][b]UNTIL[/b][/color] END-OF-INPUT-FILE
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]READ[/b][/color] MY-INPUT-FILE
      [COLOR=#6a5acd]       [/color][COLOR=#804040][b]NOT[/b][/color] [COLOR=#804040][b]AT[/b][/color] [COLOR=#804040][b]END[/b][/color]
      [COLOR=#6a5acd]         [/color][COLOR=#804040][b]MOVE[/b][/color] MY-INPUT-LINE [COLOR=#804040][b]TO[/b][/color] MY-OUTPUT-LINE
      [COLOR=#6a5acd]         [/color][COLOR=#804040][b]WRITE[/b][/color] MY-OUTPUT-LINE
      [COLOR=#6a5acd]       [/color][COLOR=#804040][b]AT[/b][/color] [COLOR=#804040][b]END[/b][/color]
      [COLOR=#6a5acd]          [/color][COLOR=#804040][b]SET[/b][/color] END-OF-INPUT-FILE [COLOR=#804040][b]TO[/b][/color] [COLOR=#804040][b]TRUE[/b][/color]
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]END-READ[/b][/color]
         [COLOR=#008080]END-PERFORM[/color]
         [COLOR=#804040][b]CLOSE[/b][/color] MY-INPUT-FILE
         [COLOR=#804040][b]CLOSE[/b][/color] MY-OUTPUT-FILE
         [COLOR=#804040][b]STOP[/b][/color] [COLOR=#6a5acd]RUN[/color].
 
Thank you very much .
I Have one last question I'm trying to convert a file that is in columns to a csv format but when i read the line can I use the initialize function to replace spaces by "," ?
I tried it but it keeps giving me an error .
 
I tried it with perform loop but it doesnt work :
IDENTIFICATION DIVISION.
PROGRAM-ID. CPYFILES.
AUTHOR. MIKROM.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MY-INPUT-FILE ASSIGN TO
"C:\Users\Hp\Documents\my_input.txt"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT MY-OUTPUT-FILE ASSIGN TO
"C:\Users\Hp\Documents\my_output.txt"
ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD MY-INPUT-FILE.
01 MY-INPUT-LINE PIC X(80).
FD MY-OUTPUT-FILE.
01 MY-OUTPUT-LINE PIC X(80).
WORKING-STORAGE SECTION.
01 END-OF-FILE PIC X VALUE SPACE.
88 END-OF-INPUT-FILE VALUE 'T'.
88 NOT-END-OF-INPUT-FILE VALUE 'F'.
01 X-COUNTER INDEX usage
01 X-OP-COUNTER index usage
PROCEDURE DIVISION.
MAIN-PARA.
OPEN INPUT MY-INPUT-FILE
OPEN OUTPUT MY-OUTPUT-FILE

SET NOT-END-OF-INPUT-FILE TO TRUE

PERFORM UNTIL END-OF-INPUT-FILE
READ MY-INPUT-FILE
NOT AT END
MOVE MY-INPUT-LINE TO MY-OUTPUT-LINE
[highlight #3465A4][PERFORM VARYING X-COUNTER FROM 1 BY 1
UNTIL X-COUNTER > 80
IF MY-INPUT-LINE(X-COUNTER:1) = ' '
CONTINUE
ELSE
MOVE MY-INPUT-LINE (X-COUNTER:1) TO
MY-OUTPUT-LINE(X-OP-COUNTER:1)
ADD 1 TO X-OP-COUNTER
END-IF
END-PERFORM.
DISPLAY 'OUTPUT-VAR:' MY-OUTPUT-LINE.][/highlight]

WRITE MY-OUTPUT-LINE
AT END
SET END-OF-INPUT-FILE TO TRUE
END-READ
END-PERFORM
CLOSE MY-INPUT-FILE
CLOSE MY-OUTPUT-FILE
STOP RUN.

 
AMCM said:
I tried it with perform loop but it doesnt work :
What does not work? What you want to do?
Post an example of your data how it looks before processing and how it should look after processing.

Please post your formatted program code between the tags [pre]
Code:
[/pre] and [pre]
[/pre]

 
HELLO , my data looks like this :
22346AROMANIACS E2005010
22346AROMANIACS E1005010
22346AROMANIACS E2005010
22346AROMANIACS E1005010
22346AROMANIACS E1005010
12344AROMANTICS E1802325
12344AROMANTICS E3002005
12344AROMANTICS E1102115
12344AROMANTICS E3002105
I want to put it in csv format .
Below is an exemple of a simple code that i tried to delete space without replacing them but it doesnt work .This the code i wanted to add to the code you gave me .
Capture_lbwsvh.jpg
Capture.JPG2_yzaefm.jpg
 
It works, but you declared both counters too small and forgot to set initial value of the second counter.
Here is corrected example
remove_spaces_gwxmia.png
 
Regarding the other example with the CSV file, post your question in the new thread.

Please do not post your program code unformated or as image, but formatted between the tags [pre]
Code:
[/pre] and [pre]
[/pre] so that we can try it right away without adjusting it manually

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top