gets() 和 sets()

本文深入探讨了C语言中用于处理字符串的库函数,包括输入函数gets()和输出函数puts()的使用方法及特性。重点介绍了如何通过这些函数进行字符串的输入与输出操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C语言编译系统提供了大量处理字符串的库函数,下面介绍几种常用的函数。使用字符串输入函数gets()和输出函数puts()时,要用#include命令将“stdio.h”头文件包含到源文件中。而使用其他的字符串处理函数时,要用#include命令将“string.h”头文件包含到源文件中。

1. 字符串输入函数gets()


调用gets()函数实现字符串的输入,其调用形式为:
gets(字符数组);
功能:从终端输入一个字符串(包括空格)赋给从字符数组起始的存储单元中,直到读入一个回车符为止。回车符读入后,不作为字符串的内容,系统将自动用’\0’替换,作为字符串结束的标志。

如:
char c[20];
gets(c);
执行上面的语句,如果输入:How are you!<CR> 
则将读入的12个字符依次存入到c[0]开始的存储单元中,并在其后自动加入一个字符串结束标志’\0’。


2.字符串输出函数puts()


调用puts()函数实现字符串的输出,其调用形式为:
puts(字符数组);
功能:将字符数组起始地址开始的一个字符串(以’\0’结束的字符序列)输出到终端,并将字符串结束标志’\0’转化成’\n’,自动输出一个换行符。 
如:
char c[ ]= "How\nare\nyou!";
puts(c);
输出结果:
How
are
you!

/// <summary> /// Represents the position of an RFID reader. /// </summary> public class RfidReaderPos()//List<PlcPoint> plcPoints { /// <summary> /// Gets or sets the track number. /// </summary> public string RfidRdrlineCode { get; set; } /// <summary> /// Gets or sets the RFID code. /// </summary> public string RfidCode { get; set; } /// <summary> /// Gets or sets the 处理类型 division 分流 print 调用mes打印 /// </summary> public string RfidProcessType { get; set; } /// <summary> /// Gets or sets the RFID UID value. /// </summary> public string RfidUIDValue { get; set; } /// <summary> /// Gets or sets the RFID tag value. /// </summary> public string RfidTagValue { get; set; } /// <summary> /// Gets or sets the RFID data value. /// </summary> public string RfidDataValue { get; set; } /// <summary> /// Gets or sets the RFID data value timestamp. /// </summary> public string RfidDataValueTimestamp { get; set; } /// <summary> /// Gets or sets the RFID position code. /// </summary> public string RfidRdrpositionCode { get; set; } /// <summary> /// Gets or sets the RFID position name. /// </summary> public string RfidRdrpositionName { get; set; } /// <summary> /// Gets or sets the RFID position description. /// </summary> public string RfidRdrpositionDesc { get; set; } /// <summary> /// Gets or sets the RFID modbus slave ID. /// </summary> public int? RfidRdrSlaveId { get; set; } // 使用可空类型以对应可能的 null 值 public int RfidRdrpollIntervalMs { get; set; } = 50; public int RfidRdrtimeoutMs { get; set; } = 6000; /// <summary> /// Gets or sets the RFID modbus IP address. /// </summary> public string RfidRdrIp { get; set; } /// <summary> /// Gets or sets the RFID modbus port. /// </summary> public string RfidRdrPort { get; set; } /// <summary> /// Gets or sets the RFID protocol, specifically for modbus. /// </summary> public string RfidRdrProtocol { get; set; } /// <summary> /// 读写PLC点位列表(JSON 存储) /// </summary> public List<PlcPoint> PlcPoints { get; set; }//= plcPoints; public SemaphoreSlim ProcessingLock { get; set; } = new (1, 1); } 参考类 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using XKJ.Communication.Wrap.Sqlite; namespace XKJ.Communication.Wrap.Models { public class PlcPoint { [PrimaryKey] [SqliteField(AutoIncrement = true, NotNull = true)] public int Id { get; set; } /// <summary> /// Gets or sets the Plc modbus address. /// </summary> [MaxLength(50)] [SqliteField(NotNull = true)] public string PlcPntAddress { get; set; } /// <summary> /// Gets or sets the Plc modbus address type :int . /// </summary> [MaxLength(20)] [SqliteField(NotNull = true)] public string PlcPntAddressType { get; set; } /// <summary> /// Gets or sets the Plc modbus address 使用定义:读信号 /// </summary> [MaxLength(100)] public string PlcPntUseDef { get; set; } /// <summary> /// Gets or sets the lib key bind with the Plc point. /// </summary> [MaxLength(50)] [SqliteField(NotNull = true)] public string PlcPntLibKey { get; set; } [SqliteField(NotNull = true)] public int PlcPntPollIntervalMs { get; set; } = 50;//ms [SqliteField(NotNull = true)] public int PlcPntTimeoutMs { get; set; } = 20000;//ms /// <summary> /// Gets or sets the Plc modbus address 可用:0 不可用 1 可用 /// </summary> [SqliteField(NotNull = true)] public int PlcPntEnabled { get; set; } = 1; /// <summary> /// Gets or sets the Plc modbus address 值: /// (1),RFID读信号(读): 0不读 1读 地址: ;RFID读完成信号(写):0 未完成 10001 完成 地址: /// (2),分流(写):1 往前,2 分流检测 地址: ;分流完成(读):0 未完成 1完成 完成后全部清0 /// </summary> [SqliteField(NotNull = false)] public int PlcPntValue { get; set; } /// <summary> /// Gets or sets the Plc modbus slave ID. /// </summary> [SqliteField(Length = 4)] public int? PlcPntSlaveId { get; set; } // 使用可空类型以对应可能的 null 值 /// <summary> /// Gets or sets the Plc modbus IP address. /// </summary> [MaxLength(15)] public string PlcPntIp { get; set; } /// <summary> /// Gets or sets the Plc modbus port. /// </summary> [MaxLength(5)] public string PlcPntPort { get; set; } /// <summary> /// Gets or sets the Plc protocol, specifically for modbus. /// </summary> [MaxLength(20)] public string PlcPntProtocol { get; set; } } } 添加注解
06-01
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace XKJ.Communication.Wrap.Models { public class PlcPoint { /// <summary> /// Gets or sets the Plc modbus address. /// </summary> public string PlcPntAddress { get; set; } /// <summary> /// Gets or sets the Plc modbus address type :int . /// </summary> public string PlcPntAddressType { get; set; } /// <summary> /// Gets or sets the Plc modbus address 使用定义:读信号 /// </summary> public string PlcPntUseDef { get; set; } /// <summary> /// Gets or sets the lib key bind with the Plc point. /// </summary> public string PlcPntLibKey { get; set; } public int PlcPntPollIntervalMs { get; set; } = 50;//ms public int PlcPntTimeoutMs { get; set; } = 20000;//ms /// <summary> /// Gets or sets the Plc modbus address 可用:0 不可用 1 可用 /// </summary> public int PlcPntEnabled { get; set; } = 1; /// <summary> /// Gets or sets the Plc modbus address 值: /// (1),RFID读信号(读): 0不读 1读 地址: ;RFID读完成信号(写):0 未完成 10001 完成 地址: /// (2),分流(写):1 往前,2 分流检测 地址: ;分流完成(读):0 未完成 1完成 完成后全部清0 /// </summary> public int PlcPntValue { get; set; } /// <summary> /// Gets or sets the Plc modbus slave ID. /// </summary> public int? PlcPntSlaveId { get; set; } // 使用可空类型以对应可能的 null 值 /// <summary> /// Gets or sets the Plc modbus IP address. /// </summary> public string PlcPntIp { get; set; } /// <summary> /// Gets or sets the Plc modbus port. /// </summary> public string PlcPntPort { get; set; } /// <summary> /// Gets or sets the Plc protocol, specifically for modbus. /// </summary> public string PlcPntProtocol { get; set; } } } 参考 public class User { [SqliteFieldAttribute(Length = 20, AutoIncrement = true, NotNull = true)] [PrimaryKeyAttribute] public int Id { get; set; } [MaxLengthAttribute(50)] public string Name { get; set; } [MaxLengthAttribute(10)] public string Password { get; set; } [SqliteFieldAttribute(Length = 2, AutoIncrement = false, NotNull = false)] public int gender { get; set; } } 优化,增加注解
06-01
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace XKJ.Communication.Wrap.Models { public class PlcPoint { /// <summary> /// Gets or sets the Plc modbus address. /// </summary> public string PlcPntAddress { get; set; } /// <summary> /// Gets or sets the Plc modbus address type :int . /// </summary> public string PlcPntAddressType { get; set; } /// <summary> /// Gets or sets the Plc modbus address 使用定义:读信号 /// </summary> public string PlcPntUseDef { get; set; } /// <summary> /// Gets or sets the lib key bind with the Plc point. /// </summary> public string PlcPntLibKey { get; set; } public int PlcPntPollIntervalMs { get; set; } = 50;//ms public int PlcPntTimeoutMs { get; set; } = 20000;//ms /// <summary> /// Gets or sets the Plc modbus address 可用:0 不可用 1 可用 /// </summary> public int PlcPntEnabled { get; set; } = 1; /// <summary> /// Gets or sets the Plc modbus address 值: /// (1),RFID读信号(读): 0不读 1读 地址: ;RFID读完成信号(写):0 未完成 10001 完成 地址: /// (2),分流(写):1 往前,2 分流检测 地址: ;分流完成(读):0 未完成 1完成 完成后全部清0 /// </summary> public int PlcPntValue { get; set; } /// <summary> /// Gets or sets the Plc modbus slave ID. /// </summary> public int? PlcPntSlaveId { get; set; } // 使用可空类型以对应可能的 null 值 /// <summary> /// Gets or sets the Plc modbus IP address. /// </summary> public string PlcPntIp { get; set; } /// <summary> /// Gets or sets the Plc modbus port. /// </summary> public string PlcPntPort { get; set; } /// <summary> /// Gets or sets the Plc protocol, specifically for modbus. /// </summary> public string PlcPntProtocol { get; set; } } } 参考 public class User { [SqliteFieldAttribute(Length = 20, AutoIncrement = true, NotNull = true)] [PrimaryKeyAttribute] public int Id { get; set; } [MaxLengthAttribute(50)] public string Name { get; set; } [MaxLengthAttribute(10)] public string Password { get; set; } [SqliteFieldAttribute(Length = 2, AutoIncrement = false, NotNull = false)] public int gender { get; set; } } 优化,增加注解,方便创建表存储到sqlite数据库
最新发布
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值