variable length line in a file deletion in C - c

I have a file main.txt and I want to delete a line in this file that contains a number passed as an argument to the program.
My code:
FILE *temp = fopen("main.txt","r");
FILE *copy = fopen("temp.txt","w+");
char line[25]; //lines won't be more than this length
while(fgets(line, sizeof(line), temp)!=NULL)
{
char *s;
s = strstr(line, argv[2]));
if(s!=NULL)
continue;
else
fputs(line,copy);
}
fclose(temp);
fclose(copy);
remove("main.txt");
rename("temp.txt", "main.txt");
Case where this fails:
Currently my file has:
echo hello 24224
echo test123 24196
echo bye 13279
When I try to remove the line with 24196, it doesn't copy the last line to the new file. Using ltrace shows that after strstr returns a non NULL value, fgets gets an emty string. How can I fix this, what is going wrong here?
__libc_start_main(0x402636, 3, 0x7ffcee2a2078, 0x4036b0 <unfinished ...>
strcmp("unmanage", "unmanage") = 0
fopen("pid1.txt", "r") = 0x19ee010
fopen("temp.txt", "w+") = 0x19ee240
fgets("echo hello 30876\n", 25, 0x19ee010) = 0x7ffcee2a1f60
strstr("echo hello 30876\n", "5785") = nil
fputs("echo hello 30876\n", 0x19ee240) = 1
fgets("echo lame 9047\n", 25, 0x19ee010) = 0x7ffcee2a1f60
strstr("echo lame 9047\n", "5785") = nil
fputs("echo lame 9047\n", 0x19ee240) = 1
fgets("echo trial 5785\n", 25, 0x19ee010) = 0x7ffcee2a1f60
strstr("echo trial 5785\n", "5785") = "5785\n"
fgets("", 25, 0x19ee010) = 0x7ffcee2a1f60
strstr("", "5785") = nil
fputs("", 0x19ee240) = 1
fgets("", 25, 0x19ee010) = 0x7ffcee2a1f60
strstr("", "5785") = nil
fputs("", 0x19ee240) = 1
fgets("", 25, 0x19ee010) = 0x7ffcee2a1f60
strstr("", "5785") = nil
fputs("", 0x19ee240) = 1
fgets("", 25, 0x19ee010) = 0x7ffcee2a1f60
strstr("", "5785") = nil
fputs("", 0x19ee240) = 1
fgets("", 25, 0x19ee010) = 0
fclose(0x19ee010) = 0
fclose(0x19ee240) = 0
remove("pid1.txt") = 0
rename("temp.txt", "pid1.txt") = 0
atoi(0x7ffcee2a320f, 0x403aca, 0, 0x7fde8ca22367) = 5785
kill(5785, SIGTERM) = 0
+++ exited (status 0) +++

Related

Add picture to objects from array table in Lua

I am creating 8 images using Cheat Engine Lua script and I want to add some pictures to the images from an array table.
the_famous = {
{photo = "AlbertEinstein.jpg", nickName = "einstein", actorName = "Albert Einstein", profession = "Physicist", creation = "Theory of relativity", nationality = "Germany", life = "14 March 1879 - 18 April 1955"},
{photo = "Mozart.jpg", nickName = "mozart", actorName = "Wolfgang Amadeus Mozart", profession = "Classical Music Composer", creation = "Symphony No.40", nationality = "Austria", life = "27 January 1756 – 5 December 1791"},
{photo = "Guevara.jpg", nickName = "guevara", actorName = "Ernesto 'Che' Guevara", profession = "Revolutionary", creation = "Cuban Revolution", nationality = "Cuba, Argentina ", life = "14 June 1928 - 9 October 1967"},
{photo = "BruceLee.jpg", nickName = "bruce lee", actorName = "Bruce Lee (Lee Jun Fan)", profession = "Martial Artist", creation = "Father of MMA", nationality = "Hongkong, America", life = "27 November 1940 - 20 July 1973"},
{photo = "Marilyn.jpg", nickName = "marilyn monroe", actorName = "Marilyn Monroe (Norma Jeane Mortenson)", profession = "Artist", creation = "The most famous sex symbol", nationality = "America", life = "1 June 1926 - 5 August 1962"}}
function start()
if #the_famous ~= 0 then
the_famous.ItemIndex = 0
local idx = the_famous.ItemIndex
for i = 1, 8 do
local idx = tableTemp.ItemIndex
local img = string.format('image1'..i) -- images name start with 'image11' till 'image18'
local pic = the_famous[i].photo
img.Picture.loadFromFile(path_to_images_stored..tostring(pic))
img.Hint = the_famous[i].nickname
idx = idx + 1
end
end
end
But the function above doesn't work. How I can do it properly?
Solved
I have change my script:
local cleft = 20
local ctop = 20
function getCategory(sender)
local cat = sender.Name
if cat == 'btnFamous' then
while #tableTemp ~= 0 do rawset(tableTemp, #tableTemp, nil) end
tableTemp = {}
for z=1, #the_famous do tableTemp[z] = the_famous[z] end
else
return nil
end
for i = 1, 24 do
local newImg = createImage(mPanel)
newImg.width = 90
newImg.height = 90
newImg.top = ctop
newImg.left = cleft
newImg.Stretch = true
newImg.Picture.loadFromFile(famous_img..tostring(tableTemp[i].photo))
newImg.showHint = true
newImg.Hint = tostring(tableTemp[i].nickName)
newImg.Name = 'pic00'..i
cleft = cleft + 100
if i == 8 or i == 16 then
cleft = 20
ctop = ctop + 100
end
newImg.OnMouseEnter = function()
hglight.visible=true
hglight.setPosition(newImg.Left-10, newImg.Top-10)
end
newImg.OnMouseLeave = function()
hglight.visible=false
hglight.setPosition(newImg.Left-10, newImg.Top-10)
end
end
end
All works properly

How to read a specific file using corona sdk

I use to program although it has been quite a few years so Im having to relearn most of it, Im trying to make it so when one of the 3 buttons are clicked it reads the file attached to the button but all i get at the moment is nul in the terminal when i run it
Ive tried moving around sections of code but I cant find anything that works
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local background = display.newImage("background.png")
background.x = centerX
background.y = centerY
local widget = require( "widget" )
-- Path for the file to read
local cluthaDirect = system.pathForFile( "clutha_Run.txt" )
local ranfDirect = system.pathForFile( "ranf_Run.txt" )
local centDirect = system.pathForFile( "cent_Run.txt" )
local function cluthaButtonEvent(event)
local phase = event.phase
if "ended" == phase then
print(cluthaFile)
end
end
local function centButtonEvent(event)
local phase = event.phase
if "ended" == phase then
print("C E N T R A L")
end
end
local function ranfButtonEvent(event)
local phase = event.phase
if "ended" == phase then
print("R A N F U R L Y")
end
end
local cluthaButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "clutha",
onEvent = cluthaButtonEvent,
}
local centButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 80,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "central",
onEvent = centButtonEvent,
}
local ranfButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 160,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "ranf",
onEvent = ranfButtonEvent,
}
-- Path for the file to read
--local cluthaDirect = system.pathForFile( "clutha_Run.txt" )
--local ranfDirect = system.pathForFile( "ranf_Run.txt" )
--local centDirect = system.pathForFile( "cent_Run.txt" )
-- Open the file handle
local cluthaFile, errorString = io.open( cluthaDirect, "r" )
local centFile, errorString = io.open( centDirect, "r" )
local ranfFile, errorString = io.open( ranfDirect, "r" )
if not cluthaFile then
-- Error occurred; output the cause
print( "cluthaFile error: " .. errorString )
else
-- Output lines
for line in cluthaFile:lines() do
print( line )
end
-- Close the file handle
--io.close( cluthaFile )
end
cluthaFile = nil
Try (not tested)
local widget = require( "widget" )
local function getContentOfFile( fileName, dir )
local contents = ''
local dir = dir or system.ResourceDirectory
local path = system.pathForFile( fileName, dir )
local file, errorString = io.open( path, "r" )
-- Open the file handle
local file, errorString = io.open( path, "r" )
if not file then
-- Error occurred; output the cause
print( "File error: " .. errorString )
else
-- Read data from file
contents = file:read( "*a" )
-- Close the file handle
io.close( file )
end
file = nil
return contents
end
local function cluthaButtonEvent( event )
local phase = event.phase
if "ended" == phase then
print( getContentOfFile("clutha_Run.txt") )
end
end
local function centButtonEvent( event )
local phase = event.phase
if "ended" == phase then
print( getContentOfFile("cent_Run.txt") )
end
end
local function ranfButtonEvent( event )
local phase = event.phase
if "ended" == phase then
print( getContentOfFile("ranf_Run.txt") )
end
end
local cluthaButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "clutha",
onEvent = cluthaButtonEvent,
}
local centButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 80,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "central",
onEvent = centButtonEvent,
}
local ranfButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 160,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "ranf",
onEvent = ranfButtonEvent,
}

LUA A function and an array, declared id, null ariable?

I have a problem with a function and an array since yesterday. It seems that lever ID is not declared or something...
Here is my code:
function tpp(leverID, from, to)
if item.uid == leverID and item.itemid == 1945 then
local count_players = #to
local store = {}
for i = 1, count_players do
local pid = getTopCreature(from[i]).uid
if (pid == 0 or not isPlayer(pid)) then
return doPlayerSendCancel(cid, 'You need ' .. count_players .. ' players to use this lever.')
end
store[i] = pid
end
for i = 1, count_players do
doSendMagicEffect(from[i], CONST_ME_POFF)
doTeleportThing(store[i], to[i], false)
doSendMagicEffect(to[i], CONST_ME_TELEPORT)
end
doTransformItem(item.uid, item.itemid + 1)
elseif item.uid == leverID and item.itemid == 1946 then
doTransformItem(item.uid, item.itemid -1)
end
end
function onUse(cid, item, fromPosition, itemEx, toPosition)
local pos = {
['pos_start'] = {
{['x'] = 1059, ['y'] = 1034, ['z'] = 7},
{['x'] = 1060, ['y'] = 1034, ['z'] = 7}
},
['pos_end'] = {
{['x'] = 1059, ['y'] = 1032, ['z'] = 7},
{['x'] = 1060, ['y'] = 1032, ['z'] = 7}
}
}
tpp(10150, pos['pos_start'], pos['pos_end'])
return true
end
I get the this error:
attempt to index global 'item' (a nil value)
I am new to lua. Can someone help me? Thanks!
Probably you forgot to pass 'item' variable to tpp() function when you called it from onUse() function.
Since there was no 'item variable in scope of tpp() function, or in its arguments, variable considered to be global, and there's no global variable with that name.

lights out game using CORONA SDK

am trying to devalope a lights out game with CORONA SDK
but am not able to figure out a way for looping it !!!
how many functions to create and the way to keep this going
here is my code (its dummy but a friend gave it to me as am trying to go on from there )
obj = nil
px = 35
py = 50
r = 22
xi = 60
yi = 60
x1y1 = display.newCircle(px+xi*0,py+yi*0,r) x1y1.id = "x1y1"
x2y1 = display.newCircle(px+xi*1,py+yi*0,r) x2y1.id = "x2y1"
x3y1 = display.newCircle(px+xi*2,py+yi*0,r) x3y1.id = "x3y1"
x4y1 = display.newCircle(px+xi*3,py+yi*0,r) x4y1.id = "x4y1"
x5y1 = display.newCircle(px+xi*4,py+yi*0,r) x5y1.id = "x5y1"
x1y2 = display.newCircle(px+xi*0,py+yi*1,r) x1y2.id = "x1y2"
x2y2 = display.newCircle(px+xi*1,py+yi*1,r) x2y2.id = "x2y2"
x3y2 = display.newCircle(px+xi*2,py+yi*1,r) x3y2.id = "x3y2"
x4y2 = display.newCircle(px+xi*3,py+yi*1,r) x4y2.id = "x4y2"
x5y2 = display.newCircle(px+xi*4,py+yi*1,r) x5y2.id = "x5y2"
x1y3 = display.newCircle(px+xi*0,py+yi*2,r) x1y3.id = "x1y3"
x2y3 = display.newCircle(px+xi*1,py+yi*2,r) x2y3.id = "x2y3"
x3y3 = display.newCircle(px+xi*2,py+yi*2,r) x3y3.id = "x3y3"
x4y3 = display.newCircle(px+xi*3,py+yi*2,r) x4y3.id = "x4y3"
x5y3 = display.newCircle(px+xi*4,py+yi*2,r) x5y3.id = "x5y3"
x1y4 = display.newCircle(px+xi*0,py+yi*3,r) x1y4.id = "x1y4"
x2y4 = display.newCircle(px+xi*1,py+yi*3,r) x2y4.id = "x2y4"
x3y4 = display.newCircle(px+xi*2,py+yi*3,r) x3y4.id = "x3y4"
x4y4 = display.newCircle(px+xi*3,py+yi*3,r) x4y4.id = "x4y4"
x5y4 = display.newCircle(px+xi*4,py+yi*3,r) x5y4.id = "x5y4"
x1y5 = display.newCircle(px+xi*0,py+yi*4,r) x1y5.id = "x1y5"
x2y5 = display.newCircle(px+xi*1,py+yi*4,r) x2y5.id = "x2y5"
x3y5 = display.newCircle(px+xi*2,py+yi*4,r) x3y5.id = "x3y5"
x4y5 = display.newCircle(px+xi*3,py+yi*4,r) x4y5.id = "x4y5"
x5y5 = display.newCircle(px+xi*4,py+yi*4,r) x5y5.id = "x5y5"
bb = {x1y1,x2y1,x3y1,x4y1,x5y1,x1y2,x2y2,x3y2,x4y2,x5y2,x1y3,x2y3,x3y3,x4y3,x5y3,x1y4,x2y4,x3y4,x4y4,x5y4,x1y5,x2y5,x3y5,x4y5,x5y5}
iClicked = 0
function click(e)
if(e.phase == "ended") then
--circleID = e.target.id
--whichCircle()
print(e.target.id)
obj = e.target
for u=1,25 do
if(obj==bb[u]) then
iClicked = u
end
end
if((iClicked-5) > 0 and (iClicked-5) < 26) then
bb[iClicked-5]:setFillColor(1,0,0)
end
if((iClicked-1) > 0 and (iClicked-1) < 26) then
bb[iClicked-1]:setFillColor(1,0,0)
end
obj:setFillColor(1,0,0)
if((iClicked+1) > 0 and (iClicked+1) < 26) then
bb[iClicked+1]:setFillColor(1,0,0)
end
if((iClicked+5) > 0 and (iClicked+5) < 26) then
bb[iClicked+5]:setFillColor(1,0,0)
end
end
end
for k=1,25 do
bb[k]:addEventListener("touch",click)
end
its all about having 25 circles and lighting them on and off but it doesnt seem to work for me
any good help will be great
Thanks
local myCircles = {}
for y = 1, 5 do
myCircles[y] = {}
for x = 1, 5 do
myCircles[y][x] = display.newCircle(px+xi*0,py+yi*4,r)
myCircles[y][x].id = .id = "x" .. x .. "y" .. y
end
end
or something like that.
Rob

TCPDF: Prints Extra Blank PDF Page on Firefox only

I am viewing pdf files for printing, using TCPDF on CakePHP 2.4.
All browsers print exactly the same expected results, in addition to Adobe Acrobat reader too. The only exception is FireFox! It adds an additional blank page. I tried all suggested solutions mentioned in other questions here (and there), and none worked.
Here is my code:
<?php
App::import('Vendor', 'xtcpdf');
header('Content-type: application/pdf');
//see tcpdf_config.php for constants definitions
$pdf = new XTCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', FALSE);
// set margins
$PDF_MARGIN_LEFT = $PDF_MARGIN_TOP = $PDF_MARGIN_RIGHT = $PDF_MARGIN_BOTTOM = 0;
$pdf->SetMargins($PDF_MARGIN_LEFT, $PDF_MARGIN_TOP, $PDF_MARGIN_RIGHT, $PDF_MARGIN_BOTTOM);
$pdf->setPrintHeader(FALSE); //$pdf->SetHeaderMargin(25);
$pdf->setPrintFooter(FALSE); //$pdf->SetFooterMargin(25);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->setFontSubsetting(FALSE);
$pdf->SetAutoPageBreak(TRUE, 0);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->addPage('L', 'Letter');
//****************** Completion Image *************************************//
$pdf->Image('/img/completion.png', $x = -10, $y = 34, $w = 240, $h = 30, $type = 'PNG', $link = '', $align = 'C', $resize = TRUE, $dpi = 900, $palign = 'C', $ismask = false, $imgmask = false, $border = 0, $fitbox = TRUE, $hidden = false, $fitonpage = TRUE);
//************************************************** TITLE Body **************************************************//
$pdf->SetFont('times', '', 20);
$title = $information['title'] ;
$pdf->writeHTMLCell($w = 0, $h = 0, $x = 0, $y = 66, $title, $border = 0, $ln = 0, $fill = 0, $reseth = true, $align = 'C', $autopadding = true);
//****************** Description *********************************//
$pdf->SetFont('times', 'I', 16);
$description = 'bla bla bla bla bla';
$pdf->writeHTMLCell($w = 0, $h = 0, $x = 0, $y = 93, $description, $border = 0, $ln = 0, $fill = 0, $reseth = true, $align = 'C', $autopadding = TRUE);
//************************** NAME **********************************//
$pdf->SetFont('times', 'B', 24);
$fullName = $information['Student']['first_name'] . ' ' . (!empty($information['Student']['middle_initial']) ? ($information['Student']['middle_initial'] . ' ') : '') . $certificate['Student']['last_name'];
$pdf->writeHTMLCell($w = 0, $h = 0, $x = 0, $y = 127, $fullName, $border = false, $ln = 0, $fill = 0, $reseth = true, $align = 'C', $autopadding = FALSE);
//***************** Line/OFFICE ************************//
$pdf->SetFont('times', 'B', 12);
$office = '__________________________________________________<br/> Bla Bla Bla <br/>Office of bla bla blat<br/>bla bla bla bla';
$pdf->writeHTMLCell($w = 0, $h = 0, $x = 3, $y = 151, $office, $border = 0, $ln = 10, $fill = 0, $reseth = FALSE, $align = 'C', $autopadding = FALSE);
//******************************* Certificate No. ************************************//
$pdf->SetFont('times', 'B', 10);
$certNo = 'No. ' . $information['Certificate']['certificate_No'];
$pdf->writeHTMLCell($w = 0, $h = 0, $x = 32, $y = 154, $certNo, $border = 0, $ln = 0, $fill = 0, $reseth = FALSE, $align = 'L', $autopadding = false);
//*************************** Date of Issuance ****************************//
$pdf->SetFont('times', '', 10);
$issueDate = 'Date of Issuance: ' . date('F, Y', strtotime($information['Certificate']['award_date']));
$pdf->writeHTMLCell($w = 0, $h = 0, $x = 31, $y = 157, $issueDate, $border = 0, $ln = 0, $fill = 0, $reseth = false, $align = 'L', $autopadding = FALSE);
echo $pdf->Output('PDF' . $information['Certificate']['certificate_number'] . '.pdf', 'I');
$pdf->endPage();
questions I looked at:
Extra blank page with TCPDF
how TCPDF prevent the extra blank page
Firefox prints extra blank page
I found no documentation, no articles, no one can explain such weird behavior.
After all, an extra blank paper won't do any harm!

Resources