fgetc is giving me incorrect character - c

I wrote a program that opens a diff file and is supposed to parse through the output. However, I can't even get to the parsing part of the algorithm. I decided to open the diff file in my main to troubleshoot what my first character in the stream is, and it doesn't match what is in the file. The first character in the file is '1', but when I run the code below, I get 49. I have no idea what has gone wrong. Can anyone guide me? Thank you in advance.
FILE *diff = fopen(diff_filename,"r");
int what;
what = fgetc(diff);
fprintf (stderr,"%d\n",what);
I tried to cast the output to a char variable, it still remains 49.

Your fpritnf() format string is wrong. If you want a character representation it should be:
fprintf (stderr,"%c\n",what);

Related

C writes unexpected characters

I've never really used C before but am trying to run this code: https://github.com/stanfordnlp/GloVe/blob/master/src/glove.c
Problem: when I read the utf8 character using this code and simply output that utf8 character, it outputs them differently.
Here is an example
µl µl
。 。
ß Ã<9f>
versión versión
◘ â<97><98>
Léon Léon
Résumé Résumé
Cancún Cancún
������ ���ï¿
The left side is what original word in fid and the right side is what this code outputs.
The fprintf is happening in line 234-237.
if (fscanf(fid,format,word) == 0) return 1;
if (strcmp(word, "<unk>") == 0) return 1;
fprintf(fout, "%s",word);
The first line reads the word from fid in format. However, format is defined as sprintf(format,"%%%ds",MAX_STRING_LENGTH);. It doesn't have any information about encoding.
My question is: How does C know which encoding to read and output? On this file, I can't find how it defines encodings like utf8, ISO-8859, etc.
How should I make this code to write left side characters?
Any comment (short is fine too!) or some keywords that I should look up will be highly appreciated! Thanks.
C doesn't know anything about whatever encoding you use for the input. The fscanf call will simply read space-delimited "characters", where each character is a single byte.

How to read a data point from a text file in C

I have a text file full of points of the following format on different lines
LONG,LONG
i can successfully read each line and print it out, but I how can I parse the string in C such that I get each long of each point on its own?
Thanks!
if you have the line already, it's easiest to use sscanf() to do this:
long a, b;
if(sscanf(line, "%ld,%ld", &a, &b) == 2)
{
/* Successfully parsed two long integers, now store them somewhere I guess. */
}
Note that it's a good idea to check the return value of sscanf(), this protects you from wrongly accepting illegal data and getting undefined results.
You can do it in multiple steps too if you need more control, as #dasblinkenlights suggested. You can use strtol() to parse the first number from the start of the line, then if that succeeds look for the comma, and then parse the second number. It can be faster than sscanf(), but I wouldn't expect too much for something this simple.
There are many solutions to this.
One is to read the line, read the first long with strtol find the position of the comma that follows with strchr, and read the second number from there.
Another solution would be to read the line, and pass it to sscanf function with the format that accepts two comma-separated LONGs.
Use the string variant of scanf() if you say you've already got the line:
char* line;
long long1;
long long2;
sscanf(line, "%ld,%ld", &long1, &long2);
Indeed as #unwind suggests in his +1 answer, it's a very good idea to check the return value of scant(), which is the number of successfully read values.

Read csv file using fscanf in C

I have to read a csv file using fscanf function (I cant use any other function like strtok_s to parse the line) and im having the following problem.
Here is the code:
fp1 = fopen (argv [1],"r");
var = fscanf (fp1,"%d,%d,%[^,]s,%[^,]s",&aux.points,%aux.titles,aux.name,aux.nation);
I'm trying to print each parameter in the screen. There is no problem with the integers and even with the first string (name) but nothing is stored in the next string (nation).
I assume that the first %[^,]s is stopping the execution of the whole fscanf function so the next string is never read. Any idea? I have tried everything but this is just not working.
Try this as the string:
"%d,%d,%[^,],%[^,]"
I eliminated the "s" because [...] acts as the specifier.
Think of the [...] as a super s.

Wrong data display on stdout: C File Operation

I wrote the following code:
main()
{
FILE *fp;
fp=fopen("ftest.txt","r");
char c, filestring[100];
int i=0;
while((c=getc(fp))!=EOF)
{
filestring[i]=c;
}
printf("str is %s",filestring);
fclose(fp);
}
The file ftest.txt contains the words Hello World.
The output displayed is not correct, it is either some other font or some other encoding.
What is the reason for this? And how do I solve this problem?
At the same time, this code runs well (shows output on stdout in "English"):
main()
{
FILE *fp;
fp=fopen("ftest.txt","r");
char c;
while((c=getc(fp))!=EOF)
{
printf("%c",c);
}
fclose(fp);
}
I need the first code to work, as I've to search in the text file. How to solve this?
The question is different from Output is not displying correctly in file operation as I'm able to "display" the correct output (as in second code), but I'm not able to write the contents of the file into a string.
Things I've tried:
1) Changing the mode in which the file is opened from "r" to "rb".
2) Changing the Notepad encoding to all available options: ANSI, UTF etc.
There are two parts of the answer:
You never increment i. This means you're just overwriting the same spot (the first space in the array) in the while loop. That's why the first value of the junk is a 'd' (the last character of your input).
The junk after the 'd' is because the array is never initialized, meaning that there is random junk already there that is never overwritten.
Another note: doing the first way would require manually adding a null byte \0 to the end of the array (either by initializing the whole thing to \0s or just after the last character is read in. This is so the string is read correctly by printf.
... and there's also a third part that's wrong here:
getc() returns an int, you're assigning it to a char before comparing it with EOF, which is defined as -1. If it just so happens that getc returns character 255, it gets assigned to a char, a signed 8 bit value, which results in, in a manner of speaking (char)-1, which then gets signed-extended to -1.

Reading and comparing numbers from txt file C

I am new to C programming, so I am having difficulties with the problem below.
I have a text file inp.txt which contains information like the following:
400;499;FIRST;
500;599;SECOND;
670;679;THIRD;
I need to type a number and my program needs to compare it with numbers from the inp.txt file.
For example, if I type 450, it's between 400 and 499, so I need write to the word FIRST to the file out.txt
I have no idea how to convert a character array to an int.
I think you'll want these general steps in your program (but I'll leave it to you to figure out how you want to do it exactly)
Load each of the ranges and the text "FIRST", "SECOND", etc. from the file inp.txt, into an array, or several arrays, or similar. As I said in the comment above, fscanf might be handy. This page describes how to use it - the page is about C++, but using it in C should be the same http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/. Roughly speaking, the idea is that you give fscanf a format specifier for what you want to extract from a line in a file, and it puts the bits it finds into the variables you specify)
Prompt the user to enter a number.
Look through the array(s) to work out which range the number fits into, and therefore which text to output
Edit: I'll put some more detail in, as asker requested. This is still a kind of skeleton to give you some ideas.
Use the fopen function, something like this (declare a pointer FILE* input_file):
input_file = fopen("c:\\test\\inp.txt", "r") /* "r" opens inp.txt for reading */
Then, it's good to check that the file was successfully opened, by checking if input_file == NULL.
Then use fscanf to read details from one line of the file. Loop through the lines of the file until you've read the whole thing. You give fscanf pointers to the variables you want it to put the information from each line of the file into. (It's a bit like a printf formatting specifier in reverse).
So, you could declare int range_start, range_end, and char range_name[20]. (To make things simple, let's assume that all the words are at most 20 characters long. This might not be a good plan in the long-run though).
while (!feof(input_file)) { /* check for end-of-file */
if(fscanf(input_file, "%d;%d;%s", &range_start, &range_end, range_name) != 3) {
break; /* Something weird happened on this line, so let's give up */
else {
printf("I got the following numbers: %d, %d, %s\n", range_start, range_end, range_name);
}
}
Hopefully that gives you a few ideas. I've tried running this code and it did seem to work. However, worth saying that fscanf has some drawbacks (see e.g. http://mrx.net/c/readfunctions.html), so another approach is to use fgets to get each line (the advantage of fgets is that you get to specify a maximum number of characters to read, so there's no danger of overrunning a string buffer length) and then sscanf to read from the string into your integer variables. I haven't tried this way though.

Resources