The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 LabTalk Forum
 Insert string and new line at begin of file
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

peter.cook

UK
356 Posts

Posted - 11/12/2003 :  08:53:34 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
HI,

I'd like to insert a string as a new line to a text file but can't find a way to do this using the fum

eg

Name1 Name2
1 25
2 26

saved as a text file. I would like to add a string as a new line to end up with eg

Name_of_test
Name1 Name2
1 25
2 26

I believe file utilities can't insert a new line at the beginning. I have tried something along the lines of :

nn=file.open(c:\temp\pete.dat,2);
file.setpos(0,nn);
file.lread(Z); // reads first row
file.setpos(0,nn);
file.lwrite("Name_of_test");
file.setpos(12,nn);
file.twrite(Z);
file.close();


but not getting anywhere...

Any help would be appreciated.

The original problem was to save a worksheet with column labels as headers and then to insert the string (nameoftest) at the top of the file ie single column on first line.

Cheers,

pete

Mike Buess

USA
3037 Posts

Posted - 11/12/2003 :  7:52:30 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Pete,

I think you're right that FUM cannot insert a line in a file. If your file is less than 8KB (the approximate # of characters that %Z will hold) you could try something like this...

file.open(c:\temp\pete.dat,2)
nn=file.size();
file.cread(Z,nn); // read entire file into %Z
file.setpos(0);
file.lwrite("Name_of_test"); // put Name_of_test at the top
file.twrite(Z); // append %Z
file.close();

If your file is too large you'll probably need to start a new file with Name_of_test, copy pete.dat a line at a time to the new file and finally replace pete.dat with the new file. The line-by-line copying is apt to take some time and you'd probably be better off seeking an Origin C solution. But first you might try this simple DOS method...

file -c "c:\temp\pete.dat" "c:\temp\temp.tmp"; // make copy of file
run -e cmd.exe /C echo Name_of_test > "c:\temp\pete.dat"; // overwrite original with the line Name_of_test
sec -p 0.1; // wait for DOS command to complete
run -e cmd.exe /C type "c:\temp\temp.tmp" >> "c:\temp\pete.dat"; // append original contents

I've assumed that you have an NT OS (NT4, Win2k, WinXP). If your OS is Win9x use command.com instead of cmd.exe in the run commands.

Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 11/12/2003 7:53:24 PM

Edited by - Mike Buess on 11/12/2003 7:54:21 PM
Go to Top of Page

peter.cook

UK
356 Posts

Posted - 11/13/2003 :  05:13:16 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Mike,

Thanks for reply.
Nice to know ~ limits for %Z.

The code crashes out at the step file.twrite(Z) - any ideas?

Yep, maybe Origin C later but my C skills don't allow so I'll need to get some assist.

Cheers,

Pete




Go to Top of Page

rlewis

Canada
253 Posts

Posted - 11/14/2003 :  3:52:42 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Though not particularly effecient the following OriginC function should do what you want in Origin 7.0SR4 and later.

To use it simply compile and link call it from LabTalk using something like

%P=Line Text to insert;//Any LT string variable other than %Z will do
InsPosn=0;// set to insert at any line you like (zero offset)
InsertLineInFile(%P,insPosn); //

You can then navigate to and select the file to be modified and the function will create a modified version of "Filename" called "Copy#_Filename" in the same directory..
It may take a while if the file is very long ...



bool InsertLineInFile(string LineToInsert, int LineNumber)
{
if (FDLOG_LT_init())
{
LabTalk.FDLOG.numTypes=1;
LabTalk.FDLOG.type1$="[All Files]*.*";
LabTalk.FDLOG.Dlgname$="Select File to be Modified";
LT_set_str("%Z","");
LabTalk.FDLOG.Open("Z");
char szBuffer[MAXFULLPATH];
LT_get_str("%Z",szBuffer,MAXFULLPATH);
string PathToFile=LabTalk.FDLOG.Path$+szBuffer;
if(PathToFile.IsFile())
{
int i=0;
string strTemp, PathToNewFile="";
do
{
i++;
strTemp.Format("%d",i);
PathToNewFile=LabTalk.FDLOG.Path$+"Copy"+strTemp+"_"+szBuffer;
}
while(PathToNewFile.IsFile());
stdioFile InStream, OutStream;
i=0;
string LineFromFile;
bool LineInsertFlag=false;
if(InStream.Open(PathToFile, file::modeRead) && OutStream.Open(PathToNewFile,file::modeCreate | file::modeWrite))
{
while(InStream.ReadString(LineFromFile))
{
if(i==LineNumber)
{
OutStream.WriteString(LineToInsert);
LineInsertFlag=true;
}
OutStream.WriteString(LineFromFile);
i++;
}
if(!LineInsertFlag)
{
OutStream.WriteString(LineFromFile);
}
InStream.Close();
OutStream.Close();
SetDataDisplayText("Mission Accomplished");
return (true);
}
}
}
SetDataDisplayText("Error Return");
return (false);
}

Go to Top of Page

peter.cook

UK
356 Posts

Posted - 11/20/2003 :  07:56:32 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Ruthven,

Thanks for this...I'll work on it.

Cheers,

pete

Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000