简体   繁体   English

如何将fscanf中的char用作int

[英]How to use char from fscanf as int

Hello this may be a stupid question, but I cannot figure it out for the world of me! 您好,这可能是一个愚蠢的问题,但我无法弄清楚我的世界! (in c) (在c中)

I'm trying to read a file like with both numbers and letter, but I want to use only the numbers in my programming stuff. 我正在尝试读取包含数字和字母的文件,但我只想在编程内容中使用数字。 That look like this: 看起来像这样:

t1 stuff 12 123 stuff
t1 stuff 12 123 stuff
t1 stuff 12 123 stuff
t2 stuff 12 123 stuff
t2 stuff 12 123 stuff
t3 stuff 12 123 stuff
t4 stuff 12 123 stuff
t4 stuff 12 123 stuff
t4 stuff 12 123 stuff
t33 stuff 12 123 stuff

(it do this all the way to t33 i just skipped from t4 to t33 to avoid big list) (它一直执行到t33,我只是从t4跳到t33以避免大名单)

The way I done it is: 我这样做的方式是:

Struct name {
char t_numbers
etc..

and: 和:

info(struct name*stuff) {
  FILE *data;
  data= fopen("thefilename", "r");
  int i=0;
  while (fscanf(file, "%s %s %d %d %s\n",stuff[i].t_numbers, etc etc...) == 5)
...

Now I tried to do t_numbers first as int and t%d instead of char, but because of the "T" in the file it can only be done as char and then s%. 现在,我尝试先将t_numbers用作int和t%d而不是char,但是由于文件中的“ T”,只能将其作为char然后再作为s%。

The problem is, how can I use the stuff[i].t_numbers as a int instead of pointer? 问题是,如何将stuff [i] .t_numbers用作int而不是指针? So it use the numbers and not the "T" in it. 因此,它使用数字而不是其中的“ T”。

Example: 例:

while (k<=10) {
        while (stuff[i].t_numbers==n) {

Will ofc not work because it's a pointer being compared to int. Will ofc无法正常工作,因为它是一个与int比较的指针。 But how to make it work? 但是如何使其工作呢? How to use those data without the "T" so i can do that. 如何在不使用“ T”的情况下使用这些数据,这样我就可以做到。

I'm extremely sorry for if it's a stupid question or if it already been asked (searched everywhere). 对于这是一个愚蠢的问题还是已经有人问过(到处搜索),我感到非常抱歉。 I am totally new to this. 我对此完全陌生。

(the codes can have spelling mistakes etc. since it was just a fast draft as example) (代码可能会有拼写错误等,因为这只是一个快速草稿而已)

Your claim about t_numbers not able to be an int because you can't use "T%d" as a format string is correct but misleading; 您关于t_numbers不能成为int因为您不能将"T%d"用作格式字符串,这是正确的,但却具有误导性; if you used the proper string, "t%d" , it works just fine. 如果您使用正确的字符串"t%d" ,它就可以正常工作。

As per my understanding you want just numbers from t1,t2,t3,... 根据我的理解,您只需要t1,t2,t3,...中的数字

I have a simple solution. 我有一个简单的解决方案。 It works only if there is one character at the beginning. 仅当开头有一个字符时,它才有效。

Change your code to read one character before int as below. 如下更改代码以读取int之前的一个字符。

info(struct name*stuff) {
  FILE *data;
  data= fopen("thefilename", "r");
  int i=0;
  char dummyT = 0;
  while (fscanf(file, "%c%d %s %d %d %s\n",&dummyT, stuff[i].t_numbers, etc etc...) == 5)
info(struct name*stuff) {
FILE *data;
data= fopen("thefilename", "r");
int i=0;
char dummyT = 0;
while (fscanf(file, "%c%d %s %d %d %s\n",&dummyT, stuff[i].t_numbers, etc etc...) ==

Shouldn't file be replaced with data? 不应将文件替换为数据吗? Also your while loop looks like it's checking to see if fscanf returns 5, might want to instead check if your file is at the end, or if possible keep a counter to loop to 33 if that's allowed, last in your name structure if you want to use %s, you need to make t_numbers an array, that should work 另外,您的while循环看起来正在检查fscanf是否返回5,可能想改为检查文件是否在末尾,或者如果可能的话,让计数器循环到33(如果允许的话),如果需要的话,在您的名称结构中最后要使用%s,您需要将t_numbers设为一个数组,该数组应该可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM