C 语言文件读写

 

    if (ferr = fopen("cerr.txt","w+"))

    {

 

       printf("打开文件成功");

 

       char aa[256];

       sprintf(aa, "%s", "hello,,,,q");

 

       fwrite(aa,strlen(aa),1,ferr);

    }

    else

    {

        printf("打开文件成败");

    }

 

 

"r"(只读) 为输入打开一个文本文件 

"w"(只写) 为输出打开一个文本文件 

"a"(追加) 为追加打开一个文本文件 

"rb"(只读) 为输入打开一个二进制文件 

"wb"(只写) 为输出打开一个二进制文件 

"ab"(追加) 为追加打开一个二进制文件 

"r+"(读写) 为读/写打开一个文本文件 

"w+"(读写) 为读/写创建一个文本文件 

"a+"(读写) 为读/写打开一个文本文件 

"rb+"(读写) 为读/写打开一个二进制文件 

"wb+"(读写) 为读/写创建一个二进制文件 

"ab+"(读写) 为读/写打开一个二进制文件 

 

 

fwrite(buffer,size,count,fp);

 

size:要读写的字节数。

 

count:要进行读写多少个size字节的数据项。

 

fflush(fp);   // 将缓冲数据立既写入

 

 

 string line;

 

 if (!getline(cin, line)) break;

 Vertex v = of_gtp_string(line);

                 istringstream in2 (s);

                 if (!(in2 >> c >> n)) return Vertex_any();

 

 

    char line2[256]={0};

    scanf("%s",line2);

 

    sscanf(line,"%c",&c);

    sscanf(line+1,"%d",&n);

 

    if ( !(c >= 'a' && c <= 'p')&&  

         !(c >= 'A' && c <= 'P'))

    {

        return Vertex_any();

    }

 

    if ( !(n >= 1 && n <= 15) )

    {

        return Vertex_any();

    }

 

 

ostringstream out;

out << endl;

return out.str ();

 

 

#define max(a,b)            (((a) > (b)) ? (a) : (b))

#define min(a,b)            (((a) < (b)) ? (a) : (b))

 

void swap(uint *x,uint *y)

{

    uint temp;

    temp=*x;

    *x=*y;

    *y=temp;

}

 

 

 

------------------------------------------

 

《The Practice of Programming》

3.1 The Markov Chain Algorithm

 

 char buf[100], fmt[10];

 /* create a format string; %s could overflow buf */
 sprintf(fmt, "%%%ds", sizeof(buf)-1);

 

        结果:fmt是 %99s

 

 

 -------------------------------------------

 

//  在n 个单词里面等概率的选择其中一个

 

char *prefix[NPREF], *w;
int i, nmatch;

nmatch = 0;
for (suf = sp->suf; suf != NULL; suf = suf->next)
    if (rand() % ++nmatch == 0) /* prob = 1/nmatch */
        w = suf->word;

 

 

--------------------------------------------

 

复制字符串

#include "stdio.h"
#include "string.h"

int main()

     char buf[100]="hello";
     char *p;

     p =  (char*)malloc(sizeof("hello")+1);
     p = strcpy(p, buf);
     printf("%s", p);

     return 0;
}

 

 

LUA

 

---------------------------------------------

 

-- 取一行中的单词

function getaword(line)
 local s, e = string.find(line, "%a+%s-%a+", 1)  -- 例如匹配"air conditioning"
 if s == nil then
  s, e = string.find(line, "%a+", 1)
 end
 if s then  -- found a word?
  return string.sub(line, s, e)
 end
end

 

 

-- 去除字串的前后空格
function trim (s)
 return (string.gsub(s, "^%s?(.-)%s*$", "%1"))
end

 

-- 文件的单词迭代器

function allwords (f)
 local line = f:read("*line")  -- current line
 local pos = 1         -- current position in the line
 return function ()
    while line do
     --print(line)
     local s, e = string.find(line, "%w+", pos)
     if s then  -- found a word?
      pos = e + 1
      return string.sub(line, s, e)
     else
      line = f:read("*line")
      pos = 1
     end
    end
    return nil
    end
end

 

-- 文件的行迭代器

function allrow(f)
 f:seek("set")
 return function ()
  local line = f:read("*line")  -- current line
  if line then
   return line
  else
   return nil
  end
   end
end

 

----------------------------------------------

 

-- markov

-----------------------------------------------

-- main.lua 

 


local Counters = {}
local Names = {}

local function hook ()
 local f = debug.getinfo(2, "f").func
 if Counters[f] == nil then -- first time 'f' is called?
  Counters[f] = 1
  Names[f] = debug.getinfo(2, "Sn")
 else -- only increment the counter
  Counters[f] = Counters[f] + 1
 end
end

local f = assert(loadfile('markov.lua'))
debug.sethook(hook, "c") -- turn on the hook for calls
f() -- run the main program
debug.sethook() -- turn off the hook

function getname (func)
 local n = Names[func]
 if n.what == "C" then
  return n.name
 end
 local lc = string.format("[%s]:%s", n.short_src, n.linedefined)
 if n.namewhat ~= "" then
  return string.format("%s (%s)", lc, n.name)
 else
  return lc
 end
end

for func, count in pairs(Counters) do
 print(getname(func), count)
end

 

 

-------------------------------------------------

-- markov.lua

 

function allwords (f)
 local line = f:read("*line")  -- current line
 local pos = 1         -- current position in the line
 return function ()
    while line do
     local s, e = string.find(line, "%w+", pos)
     if s then  -- found a word?
      pos = e + 1
      return string.sub(line, s, e)
     else
      line = f:read("*line")
      pos = 1
     end
    end
    return nil
    end
end

 

function prefix (w1, w2)
 return w1 .. " " .. w2
end

local statetab = {}

function insert (index, value)
 local list = statetab[index]
 if list == nil then
  statetab[index] = {value}
 else
  list[#list + 1] = value
 end
end

--f = io.open("c://input.txt","r") -- open input file
f = io.open("input.txt","r")
assert(f)

local N = 2
local MAXGEN = 10000
local NOWORD = "/n"

-- build table
local w1, w2 = NOWORD, NOWORD
for w in allwords(f) do
 insert(prefix(w1, w2), w)
 w1 = w2; w2 = w;
end
insert(prefix(w1, w2), NOWORD)

-- generate text
w1 = NOWORD; w2 = NOWORD -- reinitialize
for i=1, MAXGEN do
 local list = statetab[prefix(w1, w2)]

 -- choose a random item from list
 local r = math.random(#list)   -- Bug,这里的随机数是不随机的。
 local nextword = list[r]

 if nextword == NOWORD then return end
 io.write(nextword, " ")
 w1 = w2; w2 = nextword
end

f:close()

 

-------------------------------------------------

-- input.txt

 

the more we try the more we do

 

-------------------------------------------------

 

Haskell

 

--------------------------------------------------

-- main.hs

 

module Main where

 

tidySecond :: [a] -> Maybe a;

tidySecond (_:x:_) = Just x  -- 亮点!

tidySecond _ = Nothing

 

tt = tidySecond [1,2]

 

main = do

    print tt

 

    {-

     print "hello,world!"

     tidySecond [1,2] -- Just 2

    -}

--------------------------------------------------

Haskell 安装package 的方法:

-- 先进入下载回来的package 目录

cabal configure
cabal build
cabal install

ghc-pkg list 


-- Haskell 随机数  


 -- haskell98
import Random


main = do a <- drawInt 1 10
   b <- drawInt 1 10
   x <- drawDouble 0.0 1.0
   print a
 print b
 print x


drawInt :: Int -> Int -> IO Int
drawInt x y = getStdRandom (randomR (x,y))
  
drawDouble :: Double -> Double  -> IO Double
drawDouble x y = getStdRandom (randomR (x,y))

 

 -----------------------------------------------------


lua


-- 保留两位小数
--[[
a = (math.floor(9.005*10^2 +0.5)) / (10^2)
b = (math.ceil(-9.005*10^2-0.5)) / (10^2)
print(a,b)
--]]
function round(n, d)
local e = 10^d
if (n > 0) then
return (math.floor(n*e+0.5)) / (e)
else
return (math.ceil(n*e-0.5)) / (e)
end
end
---[[
c = round(9.005, 2)
d = round(-9.005, 2)
print(c, d)
--]]

----------------------------------------------------------------

lua

-- 逐一输出字串中的每一字符的十六进制值
for _, b in pairs{string.byte('\r\nabcd', 1, -1)} do
print (string.format("%02X ", b))
end


-- 字串转数字

tonumber(content, 10)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值