Solved

why do i have bug with [Can't open File]

Posted on 2000-02-29
5
296 Views
Last Modified: 2010-04-16
Can you tell em where do i have bug?

Program donors_app;
Uses Crt;

Const
  db_stuff = 'c:\-r76\db_stuff.bin';           {faila atrash. vieta}
  Name_signs = 15;                             {varda garums simbolos}
  SurName_signs = 25;                          {SurVaarda garums simbolos}
  Vec_signs = 3;
  n = 100;                                     {ierakstu skaits (max) }

Type
 donors = record
   Name: string[Name_signs];                   {Vards 15 simb. gars}
   SurName: string[SurName_signs];             {SurVaards 25 simb. gars}
   RegNum: integer;                            {Reg. Numura garums 8 simb.}
   Vecums: String[Vec_signs];                  {Vecums 0 - 150}
   Dzimums: string[4];                            {dzimums - vir./siev.}
End;


FFile = file of donors;                 {faila tips}
rec = array[1..n] of donors;            {masivu tips, pec faila tipa glab.}

var fails: FFile;                       {failam dotais nosaukums}
    kop_rec: integer;                   {kopejo ierakstu skaits}


Function Pareizs_REC(i:integer):boolean;   {parbauda vai eksistee ieraksts}
var g: rec;                                {ierakstu skaits}
    j:integer;                             {skaita ierakstus}
begin
 j:=1;                                     {ierkast nr. 1}
 Pareizs_Rec:= true;                       {ieraksts eksitee}
 {$I-}
 Close(fails); {}                          {aizver failu}
 {$I+}
 Assign(fails,db_stuff);                   {faila/lokacijas asigneshana}
 Reset(fails);                             {izdzeshana}
 While ((Eof(fails))<>true) do
   begin
     read(fails,g[j]);                     {nolasa no faila j-to iekatu}
     inc(j);                               {noapalo j, lai naf bugu}
   end;
 if i+1>j then Pareizs_REC:=false;         {parbauda vai eksiste nakamais ieraksts}
End;

Procedure MakeRec;                             {recordu veidoshana}
var g: rec;
    Y: char;   s,temp: string;   ok: boolean; a,i,b,rez:integer;
      TempSTR,chose: String;  Code,TempG: Integer;
      Label 12; Label 13;
Begin
{$I-}
Close(fails);{}
{$I+}
Assign(fails,db_stuff);
{Reset(fails);}
If IOResult <> 0 then Rewrite(fails); {}
Seek(fails,FileSize(fails));                    {meklee faila beigas}
inc(kop_rec);
ClrScr;
Writeln('Pec neprecizas vertibu ievadishanas, tas bus jaaievada atkartoti!');
GotoXY(3,22);
Write('-press enter-'); Readln;
ClrScr;
Writeln('Ievadiet personas vardu - ',Name_signs,' simboliem');
GotoXY(3,22);
Write('-ievadiet- ');
ReadLn(g[kop_rec].Name);           {ieglabaa masivaa name}
ClrScr;
Writeln('Ievadiet personas uzvardu - ',SurName_signs,' simboliem');
GotoXY(3,22);
Write('-ievadiet- ');
ReadLn(g[kop_rec].SurName);                        {ieglabaa masivaa SurName}

ClrScr;                                            {izveido reg numuru}
Writeln('reg-num tiks noteikts automatiski');
GotoXY(3,22);
Write('-press enter-');
ReadLn;
 Randomize;
    rez := Random(9999);
    if rez<=1000 then rez:=rez+1000;
 ClrScr;
    Write('Jusu registracijas numurs--> ', rez);
    GotoXY(3,22); Write('-press enter-');
    g[kop_rec].RegNum := rez;
readln;

ok:=false;
While ok <> true do                      {ievadam datus -vecums}
begin
13: ClrScr;
    Writeln('Ievadiet personas vecumu: ');
    GotoXY(3,22); Write('-ievadiet- ');
    ReadLn(temp);
      begin
       a:=0;
        for i:=1 to length(temp) do begin
         if (ord(temp[i])>=48) and (ord(temp[i])<=57)
          then b:=1  {ASCII kodaa sk}
        else a:=a+1;
     if a>=1 then goto 13
       else ok:=true;
       g[kop_rec].vecums:=temp;
  end;
 end;
end;

Repeat                       {parbauda ievadisha par siev/vir}
   ClrScr;
    Writeln('Izvelas vajadzigo: [0-Siev/1-Vir]');
    GotoXY(3,22); Write('-ievadiet- ');
      ReadLn(TempSTR);
      Val(TempSTR,TempG,Code);
    Until ((Code = 0) And (TempG In [0..1]));
       if tempStr = '1' then chose:='vir.';
       if tempStr = '0' then chose:='siev.';
      g[kop_rec].Dzimums:=chose;
ClrScr;
Writeln('Saglabat datus failaa?  [Y/N]');
GotoXY(3,22); Write('-izvele- ');
readln(Y);
if (Y = 'Y') or (Y = 'y') then write(fails,g[kop_rec]);
End;

Procedure PrintRec;
var g: rec;
i,j: integer;
Begin
ClrScr;
{$I-}
Close(fails);{}
{$I+}
Assign(fails,db_stuff);
Reset(fails);{}
i:=1; j:=0;
While ((Eof(fails))<>true) do
 begin
   inc(j);
   Read(fails,g[i]); {}
   Writeln('nr:             ',i);
   Writeln('name:           ', g[i].name);
   Writeln('surname:        ', g[i].surname);
   Writeln('reg nr:         ', g[i].regnum);
   Writeln('vecums:         ', g[i].vecums);
   Writeln('dzimums:        ', g[i].dzimums);
   writeln;
 end;
readln;
End;


begin
MakeRec;
PrintRec;
end.


0
Comment
Question by:fr0x
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Hypo
ID: 2569752
I can run your code without any errors. My guess is either that the file c:\-r76\db_stuff.bin doesn't exist or that you don't have access to write to the disk. e.g. if you're on a network, you might not have access to write files on C:

other than that, I don't know why your code throws an error at you!
0
 

Author Comment

by:fr0x
ID: 2569917
Hmz.. I forget to told, taht i have bug with ..!!

I can add data and print data on screen! but when i start this process again.. to add new data to file *.bin!

all data is been erased and i can save just the last data entry!

why?

i had to get such a result:
Number 1.
Name sjkhdj
surname lkss
....

Number 2
Name jdewh
Surname kjxhid
....

Number 3
Name jdesdesd
Surname kjsdfsfe
....

etc.

Can you tell me where is a problem?
0
 
LVL 3

Accepted Solution

by:
sumant032199 earned 50 total points
ID: 2569942
hello fr0x,
There can be following reasons

1. File does not exist.
2. PAth does not exist.
3. Drive does not exist.
4. File readonly.
5. Shared file under multiuser / multitasking system.
0
 

Author Comment

by:fr0x
ID: 2570009
I have this error:
1. File does not exist.

cn you tell how to solve this?
0
 
LVL 12

Expert Comment

by:Hypo
ID: 2570611
Ok, I've made a few changes to your code... At two points you started the procedures (Make_Rec and Print_Rec) by closing a file (fails), I moved those line to the end of the procedures instead. And I think it works fine now!
(I would have posted this as an answer if I it were'nt already locked!)


Take a look at it, Does it work like you wanted it to do?

Program donors_app;
Uses Crt;

Const
  db_stuff = 'e:\program\pascal\test2.pas';           {faila atrash. vieta}
  Name_signs = 15;                             {varda garums simbolos}
  SurName_signs = 25;                          {SurVaarda garums simbolos}
  Vec_signs = 3;
  n = 100;                                     {ierakstu skaits (max) }

Type
 donors = record
   Name: string[Name_signs];                   {Vards 15 simb. gars}
   SurName: string[SurName_signs];             {SurVaards 25 simb. gars}
   RegNum: integer;                            {Reg. Numura garums 8 simb.}
   Vecums: String[Vec_signs];                  {Vecums 0 - 150}
   Dzimums: string[4];                            {dzimums - vir./siev.}
End;


FFile = file of donors;                 {faila tips}
rec = array[1..n] of donors;            {masivu tips, pec faila tipa glab.}

var fails: FFile;                       {failam dotais nosaukums}
    kop_rec: integer;                   {kopejo ierakstu skaits}


Function Pareizs_REC(i:integer):boolean;   {parbauda vai eksistee ieraksts}
var g: rec;                                {ierakstu skaits}
    j:integer;                             {skaita ierakstus}
begin
 j:=1;                                     {ierkast nr. 1}
 Pareizs_Rec:= true;                       {ieraksts eksitee}
 {$I-}
 Close(fails); {}                          {aizver failu}
 {$I+}
 Assign(fails,db_stuff);                   {faila/lokacijas asigneshana}
 Reset(fails);                             {izdzeshana}
 While ((Eof(fails))<>true) do
   begin
     read(fails,g[j]);                     {nolasa no faila j-to iekatu}
     inc(j);                               {noapalo j, lai naf bugu}
   end;
 if i+1>j then Pareizs_REC:=false;         {parbauda vai eksiste nakamais ieraksts}
End;

Procedure MakeRec;                             {recordu veidoshana}
var g: rec;
    Y: char;   s,temp: string;   ok: boolean; a,i,b,rez:integer;
      TempSTR,chose: String;  Code,TempG: Integer;
      Label 12; Label 13;
Begin
{$I-}
Assign(fails,db_stuff);
Reset(fails);
If IOResult <> 0 then Rewrite(fails); {}
Seek(fails,FileSize(fails));                    {meklee faila beigas}
inc(kop_rec);
ClrScr;
Writeln('Pec neprecizas vertibu ievadishanas, tas bus jaaievada atkartoti!');
GotoXY(3,22);
Write('-press enter-'); Readln;
ClrScr;
Writeln('Ievadiet personas vardu - ',Name_signs,' simboliem');
GotoXY(3,22);
Write('-ievadiet- ');
ReadLn(g[kop_rec].Name);           {ieglabaa masivaa name}
ClrScr;
Writeln('Ievadiet personas uzvardu - ',SurName_signs,' simboliem');
GotoXY(3,22);
Write('-ievadiet- ');
ReadLn(g[kop_rec].SurName);                        {ieglabaa masivaa SurName}

ClrScr;                                            {izveido reg numuru}
Writeln('reg-num tiks noteikts automatiski');
GotoXY(3,22);
Write('-press enter-');
ReadLn;
 Randomize;
    rez := Random(9999);
    if rez<=1000 then rez:=rez+1000;
 ClrScr;
    Write('Jusu registracijas numurs--> ', rez);
    GotoXY(3,22); Write('-press enter-');
    g[kop_rec].RegNum := rez;
readln;

ok:=false;
While ok <> true do                      {ievadam datus -vecums}
begin
13: ClrScr;
    Writeln('Ievadiet personas vecumu: ');
    GotoXY(3,22); Write('-ievadiet- ');
    ReadLn(temp);
      begin
       a:=0;
        for i:=1 to length(temp) do begin
         if (ord(temp[i])>=48) and (ord(temp[i])<=57)
          then b:=1  {ASCII kodaa sk}
        else a:=a+1;
     if a>=1 then goto 13
       else ok:=true;
       g[kop_rec].vecums:=temp;
  end;
 end;
end;

Repeat                       {parbauda ievadisha par siev/vir}
   ClrScr;
    Writeln('Izvelas vajadzigo: [0-Siev/1-Vir]');
    GotoXY(3,22); Write('-ievadiet- ');
      ReadLn(TempSTR);
      Val(TempSTR,TempG,Code);
    Until ((Code = 0) And (TempG In [0..1]));
       if tempStr = '1' then chose:='vir.';
       if tempStr = '0' then chose:='siev.';
      g[kop_rec].Dzimums:=chose;
ClrScr;
Writeln('Saglabat datus failaa?  [Y/N]');
GotoXY(3,22); Write('-izvele- ');
readln(Y);
if (Y = 'Y') or (Y = 'y') then write(fails,g[kop_rec]);
close(fails);
{$I+}
End;

Procedure PrintRec;
var g: rec;
i,j: integer;
Begin
ClrScr;
{$I-}
Assign(fails,db_stuff);
Reset(fails);{}
i:=1; j:=0;
While ((Eof(fails))<>true) do
 begin
   inc(j);
   Read(fails,g[i]); {}
   Writeln('nr:             ',i);
   Writeln('name:           ', g[i].name);
   Writeln('surname:        ', g[i].surname);
   Writeln('reg nr:         ', g[i].regnum);
   Writeln('vecums:         ', g[i].vecums);
   Writeln('dzimums:        ', g[i].dzimums);
   writeln;
 end;
readln;
Close(fails);{}
{$I+}
End;


begin
MakeRec;
PrintRec;
end.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to Use TTouchKeyboard inherit 1 1,504
Indy TIdHTTP POSTs getting slow responses 6 1,082
interacting with web form 4 151
proper way to parse text with delphi 7 159
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Adults who share images on social media aren’t the only ones who need to worry about their privacy. Our culture’s tendency to share every move and celebration affects the privacy of our children, too.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question