mystr_len

#include <stdio.h>

int mystr_len(char *Desstr)
{
 if(Desstr == NULL) return 0;
 if(*Desstr == '\0')
 {
 
   return 0;
 
 }

else return (1+(mystr_len(++Desstr)));注意是先++
}

int main()
{

char *str = "hello";
printf("%d",mystr_len(str));
return 0;
}

转载于:https://www.cnblogs.com/liuweilinlin/archive/2012/08/30/2664340.html

--函数名称:my_getedataLen_Type --功能说明:获取对应数据的类型和长度 --形 参:data:待传入的数据 --返 回 值:datalen:数据的长度 data_type:数据的类型 function my_getdataLen_Type(data) local datalen = -1 local data_type = type(data) if data_type == 'number' then -- 将数字转换为字符串后再计算长度 datalen = string.len(tostring(data)) elseif data_type == 'string' then datalen = string.len(data) elseif data_type == 'table' then datalen = #data end return datalen, data_type end --函数名称:my_split --功能说明:将一个字符串按照指定的分隔符分割成多个子字符串,并将这些子字符串存储在一个表(数组)中返回。 --形 参:str:字符串 pat:分隔符 --返 回 值:返回存储所有子字符串的表 t function my_split(str, pat) local i = 1 -- 初始化计数器 i,用于记录分割后的子字符串数量 local t = {} -- 创建一个空表 t,用于存储分割后的子字符串 local last_end = 0 -- 初始化变量 last_end,记录上一个分隔符结束的位置 local s, e = string.find(str, pat, 1) -- 在字符串 str 中从位置 1 开始查找分隔符 pat,返回分隔符的起始位置 s 和结束位置 e while s -- 如果找到了分隔符(s 不为 nil) do table.insert(t, string.sub(str, last_end + 1, last_end + s - last_end - 1)) -- 截取从 last_end + 1 到分隔符起始位置 s - 1 ?淖幼址⒔洳迦氲奖?t 中 last_end = e -- 更新 last_end 为当前分隔符结束的位置 e s, e = string.find(str, pat, last_end + 1) -- 在字符串 str 中从 last_end + 1 的位置开始继续查找分隔符 pat i = i + 1 -- 增加计数器 i end if last_end <= #str --如果 last_end 小于等于字符串 str 的长度(即还有未处理的剩余部分) then cap = string.sub(str, last_end + 1) -- 截取从 last_end + 1 到字符串结尾的子字符串 table.insert(t, cap) -- 将该子字符串插入到表 t 中 end return t -- 返回存储所有子字符串的表 t end --函数名称:my_ShowRecord_FromFile --功能说明:用于处理从文件中读取的字符串数据,按行分割后,去除每行中的回车符和换行符,并在每行末尾添加分号,最后将处理后的数据添加到记录显示区域。 --形 参:mystr:读取的字符串 --返 回 值:无 function my_ShowRecord_FromFile(mystr) local line_data = my_split(mystr, '% ') -- -- 调用 my_split 函数,将 mystr 字符串按照换行符 '\n' 分割成多个子字符串,存储在 line_data 表中 for i = 1, #(line_data) -- 遍历 line_data 表中的每个元素 do if line_data[i] ~= nil -- 如果当前元素不为空 then feed_dog() -- 调用 feed_dog 函数(可能是用于防止系统“看门狗”超时等操作) -- 去除当前行数据中的回车符 ' ' 和换行符 '\n' line_data[i] = string.gsub(line_data[i], "% ", '') line_data[i] = string.gsub(line_data[i], "%\n", '') --line_data[i] = line_data[i]..';' -- 在当前行数据末尾添加分号 ';' -- 将处理后的行数据添加到记录显示区域 sc_ShowRecord 中,格式为 '$NUM;' 加上处理后的行数据 set_history_graph_value(26,6,line_data[i]) set_value(26,9,line_data[i]) end end end function my_read_filedata(file) local count = 0 --每次要读取到的字节数 local read_cnt = 0 --要读取多少次 多少块 local offset = 0 --读取偏移地址 local all_byte = 0 --要读取的所有的字节数 local read_byte_Tb = {} --用于存储从文件中读取的原始字节数据 local read_char_Tb = {} --用于将字节数据转换为字符数据 local read_str = '' --用于存储最终拼接成的字符串形式的文件内容。 local open_state = file_open(file, FA_READ) --以只读的方式进行打开 if open_state == true then set_text(26, 5, 'open on') --获取当前文件大小 all_byte = file_size() if all_byte > 0 then read_cnt = math.modf(all_byte/WriteOnceSize) --计算出完整的有多少块 if all_byte % WriteOnceSize > 0 then read_cnt = read_cnt + 1 --最后一块未满的一块 end for i = 1, read_cnt do --复位读字节数组 read_byte_Tb = {} --用于存储从文件中读取的原始字节数据 read_char_Tb = {} --用于将字节数据转换为字符数据 --计算读取的偏移位置 offset = (i - 1) * WriteOnceSize local offst_result = file_seek(offset) --文件偏移失败 if offst_result == false then set_text(26, 5, 'offest fail') break else set_text(26, 5, 'offest on') end --计算本次读的个数 count = WriteOnceSize if i == read_cnt --最后一块 then if all_byte % WriteOnceSize > 0 then count = all_byte % WriteOnceSize end end --读取字节转换为字符并拼接成字符串 read_byte_Tb = file_read(count) if #(read_byte_Tb) > 0 then set_text(26, 5, 'read ok') for j = 0, #(read_byte_Tb) do read_char_Tb[j + 1] = string.char(read_byte_Tb[j]) --将当前字节的数据转换为字符 从下标1开始 Lua 中数组索引通常从 1 开始 end read_str = read_str..table.concat(read_char_Tb) --将read_char_Tb数组中的每一个数据拼接到一个字符串中 追加到 read_str 字符串变量中,从而逐步构建完整的文件内容字符串 elseif read_byte_Tb ==nil --显示文件读取错误的提示信息,提醒用户重新尝试 then set_text(26, 5, 'read fail') break; end end my_ShowRecord_FromFile(read_str) else --区域显示文件不存在或无内容的提示信息,提醒用户检查 SD 卡中的文件 set_text(26, 5, 'dont exist') end else set_text(26, 5, 'open fail') end --关闭文件 file_close() end --函数名称:my_write_filedata --功能说明:使对应的要写入的数据的类型值 转成数值类型存到发送函数中 利用file_write() :函数进行发送 --形 参:file:文件的绝对路径 data:要写入SD卡的数据 open_mode:打开的模式 --返 回 值:无 function my_write_filedata(file, data, open_mode) local count = 0 -- 当前写入块大小(每次) local write_cnt = 0 -- 总写入次数 要写入几次才能写完 local seek_ops = 0 -- 写入偏移量 local all_byte = 0 -- 原文件大小(追加模式用) 在原有文件大小的后面进行添加 --获取待写入数据的数据类型和长度 local wrire_len, data_type = my_getdataLen_Type(data) local write_byte_Tb = {} -- 写入缓存 --打开文件 local open_state = file_open(file, open_mode) if open_state == true then set_text(28, 6, 'open on') --获取当前文件大小,仅在追加文件末尾写入执行 追加模式获取原文件大小 if open_mode == add_write then all_byte = file_size() end --若有数据要写入 if wrire_len > 0 then --计算data要写多少次 计算写入次数 也就是有多少块数据 write_cnt = math.modf(wrire_len / WriteOnceSize) if wrire_len % WriteOnceSize > 0 then write_cnt = write_cnt + 1 end --循环分块写入 for i = 1, write_cnt do --复位写字节数组 清空临时表 write_byte_Tb = {} --计算写的位置 seek_ops = (i - 1) * WriteOnceSize +all_byte local offst_result = file_seek(seek_ops) --若文件偏移失败 if offst_result == false then --提示信息 set_text(28, 6, 'offst off') break else set_text(28, 6, 'offst ok') end --计算本次写的个数(字节数) count = WriteOnceSize --当前写入块的大小 if i == write_cnt --最后一块 then if wrire_len % WriteOnceSize > 0 then count = wrire_len % WriteOnceSize --当前写入块的大小 end end --填充写入字节数组 准备写入数据 for j = 1, count do --if data_type == 'string' or data_type == 'number' if data_type == 'string' then --字符串类型,将每个字符转换为字节数组 -- 字符串转字节 --local str = (data_type == 'string') and data or tostring(data) --write_byte_Tb[j - 1] = string.byte(str, ((i - 1) * WriteOnceSize + j), ((i - 1) * WriteOnceSize + j)) --tonumber():将结果显式转换为数值类型 --字符串类型,将每个字符转换为字节数组 填写到发送函数中 write_byte_Tb[j - 1] = tonumber(string.byte(data, ((i - 1) * WriteOnceSize + j), ((i - 1) * WriteOnceSize + j))) elseif data_type == 'table' then --数组类型,字节赋值 -- 直接使用字节数组 write_byte_Tb[j - 1] = data[((i - 1) * WriteOnceSize + j)] end end -- 执行写入 local IswriteOK = file_write(write_byte_Tb) if IswriteOK == false then set_text(28, 6, 'write off') else set_text(28, 6, 'write ok') end end end else set_text(28, 6, 'open fail') end --关闭文件 file_close() end local gas =0 --保存采集到气体的浓度值 --定时回调函数,系统每隔1秒钟自动调用 function on_systick() gas= get_value(0,14) --local tabl ={} --tabl[1]=a --local str = " ".. tostring(gas) local str = tostring(gas).." " my_write_filedata(sd_dir..'/'..'1.txt', str, add_write) --读取 my_read_filedata(sd_dir..'/'..'1.txt') end 现在读写都能够正常运行 但发现了一个bug 现在sd已经有下入的浮点数数据 我想要利用函数my_read_filedata进行只读 结果是卡在一个数进行不动了 这是什么原因
最新发布
07-03
6-1 从文件读取字符串(*) 分数 10 作者 李祥 单位 湖北经济学院 请编写函数,从文件中读取字符串。 函数原型 void FGetStr(char *str, int size, FILE *file); 说明:参数 str 为指示字符数组起始地址的指针,size 为数组尺寸,file 为文件指针。函数从文件输入的字符串(以换行符 '\n' 结束)到字符数组中,并在字符末尾添加字符串结束标记 '\0'。显然,字符串的最大长度应为 size - 1,为字符串结束标记 '\0' 预留空间。若用户输入的字符过多,则函数最多读取 size - 1 个字符,剩余字符仍留在缓冲区中,可以继续被后面的输入函数读取。 要求:不得把文件中的换行符 '\n' 也保存到字符串中。 裁判程序 #include <stdio.h> #include <string.h> void FGetStr(char *str, int size, FILE *file); int main() { FILE *f; char a[10], b[10]; f = fopen("MyStr.txt", "r"); if (f) { FGetStr(a, 10, f); FGetStr(b, 10, f); puts(a); puts(b); fclose(f); } return 0; } /* 你提交的代码将被嵌在这里 */ 创建文本文件 MyStr.txt,复制下面的内容。 MyStr.txt * Bob Mary 输出样例1 Bob Mary 修改文本文件 MyStr.txt,复制下面的内容。 MyStr.txt * Constantine 输出样例2 Constanti ne 修改文本文件 MyStr.txt,复制下面的内容。 MyStr.txt * Francisco Stevenson 输出样例3 Francisco Stevenson 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB C (gcc) 1 2 3 4 5 6 7 void FGetStr(char *str, int size, FILE *file) { fgets(str, size, file); int len = strlen(str); if (len > 0 && str[len - 1] == '\n') { str[len - 1] = '\0'; } }
03-10
1. 直接写出程序运行结果 int i,j,s;  printf("\n");  for (i=1;i<10;i++)   { for(j=1;j<10;j++)     {      s=i*j;      printf("%d*%d=%3d",i,j,s);     }    printf("\n");   } 2. 直接写出程序运行结果 a = [9,6,5,4,1] N = len(a) for i in range(len(a) / 2): a[i],a[N-i-1] = a[N-i-1],a[i] print a 3.下面程序运行时,输入69 90 86 ,写出程序的输出结果。 scores = [] for i in range(3): x = eval(input('请输入学生的百分制成绩:\n')) scores.append(x) scores.sort() print(scores) 4.下面程序运行时输入:hello world 123# 请写出程序运行结果。 s = input('input a string:\n') letters = 0 space = 0 digit = 0 others = 0 for c in s: if c.isalpha(): letters += 1 elif c.isspace(): space += 1 elif c.isdigit(): digit += 1 else: others += 1 print('char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,others)) 5. 直接写出程序运行结果 w = 1 def func(): w = 2 print(w) w = 3 func() print(w) 6.写出下面程序绘制的图形及其颜色。 import turtle turtle.bgcolor("red") turtle.fillcolor("yellow") turtle.color('yellow') turtle.begin_fill() turtle.up() turtle.goto(-200,180) turtle.down() for i in range (5): turtle.forward(150) turtle.right(144) turtle.end_fill() 7.直接写出程序运行结果 s = [1,2] while(len(s)<5): s.append(s[len(s)-1]+s[len(s)-2]) 8. 直接写出程序运行结果 num = 1 for i in range(4): print(num ,end=“,”) num += 2 9.写出下面程序的运行结果。 def hanoi(n, from_, with_, to_): if n > 0: hanoi(n-1, from_, to_, with_) print(from_+"->"+to_) hanoi(n-1, with_, from_, to_) hanoi(3,'A','B','C') 10. a.txt文件的内容为: Hi Python bye! 请写出如下代码的输出: file = open(‘a.txt’) file.readline() mystr = file.readline( ) print(mystr) file.close() print(s)
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值