C outputting "{" when reading from file - c

I have written this code using Xcode to read from a file:
int main (int argc, char *argv[])
{
FILE *fp = fopen("hello.rtf", "r");
printf("%c\n", fgetc(fp));
fclose(fp);
if (fp == NULL)
{
printf("Could not open file!");
return 1;
}
// insert code here...
return 0;
}
The character that I get is "{" and it is not the first character in the file.

The RTF spec says that the first character in an RTF file should be {, so it seems that you are getting the expected result. Bear in mind that a word processing software will not show you the exact characters in the file, but it will show you the formatted text which has been described by the markup characters in the file.
To see the exact characters in the file you could output it with cat (POSIX) or type (DOS / Windows command prompt).
The if (fp == NULL) check should go immediately after the fopen line; by the end it's too late.

Related

Problems about yyin and yyout using in lex for lexical analysis

This is main function for lexical analysis.
If I use yyin for getting data from file, yyout execute and writes to file, but when I give codes from terminal yyout isn't working. How can I solve that?
int main(int argc, char *argv[])
{
extern FILE *yyin, *yyout;
char *input;
printf("flexing there\n argc=%d\n argv[0]=%s argv[1]=%s \n",argc,argv[0],argv[1]);
if(argc > 1 && strstr(argv[1],".g++") != NULL){
printf("we will read your file = %s\n",argv[1]);
yyin = fopen(argv[1], "r");
}
else if(argc > 1){
printf("your file type is wrong ,must be .g++ type\n");
return 0;
}
else{
printf("enter your code please\n ");
}
yyout = fopen("outputlex.txt", "w");
yylex();
return 0;
}
I use yyout at rule section like that
%%
"(" fprintf(yyout,"OP_OP\n");
%%
In C, when you write to a file, the output is kept in a buffer until there is enough data to make it worthwhile writing, typically around 4kb. So if you just write a few bytes, you won't see anything in the file until it is closed.
By contrast, when you write to the terminal, the output is usually only buffered until a newline character is written. And if you write to stderr, the output is always immediate.
You can change the buffering for a stream by calling setvbuf right after the fopen. You can also force the stream's buffer to be sent using fflush.

Why are there extra spaces in the file after copying text from one file to another in C?

char c, cp;
FILE *input_file, *output_file;
input_file = fopen("d:\\input.txt","r");
output_file = fopen("d:\\output.txt", "w");
if(input_file==NULL){
printf("cannot open the input.txt file.");
exit(0);
}
if(output_file == NULL){
printf("cannot open the output.txt file.");
exit(0);
}
cp = fgetc(input_file);
while(cp!=EOF){
fputc(cp,output_file);
cp=fgetc(input_file);
}
c = fgetc(output_file);
while(c!=EOF){
printf("%c",c);
c=fgetc(output_file);
}
fclose(input_file);
fclose(output_file);
getch();
This is the code that I used while copying a text file.
In the input.txt file I have written "Hello how are you".
After executing the code, the text "Hello how are you" is copied to the output.txt file
but there are more than a hundred spaces after the copied text.
After the code below the program is not working:
cp = fgetc(input_file);
while(cp!=EOF){
fputc(cp,output_file);
cp=fgetc(input_file);
}
Code below the above code is not working.
What is happening? Please explain in detail. I am a beginner in C.
You must define c and cp as int not as char. EOF is defined as an integer value which is distinguishable from any char which may e.g. be read by fgetc(), which returns a value in the unsigned char range or EOF, not necessarily a value in the char range. (credits to #chux).
So while( cp != EOF ) might not become true if cp is a char.
And for the second issue: if you want to read what you have written you must
open output.tx with mode "w+". "w" only permits writing, "w+" allows reading too but like "w" creates the file if it doesn't exist and truncates it if it does. See the man page for further options and details.
call rewind(output_file) or fseek(output_file, 0, SEEK_SET) between reading and writing.
you must close your output file to commit changes and be able to read a second time:
cp = fgetc(input_file);
while(cp!=EOF){
fputc(cp,output_file);
cp=fgetc(input_file);
}
fclose(output_file);
// .....
c = fgetc(output_file);

C fopen and fgets returning weird characters instead of file contents

I am doing a coding exercise and I need to open a data file that contains lots of data. It's a .raw file. Before I build my app I open the 'card.raw' file in a texteditor and in a hexeditor. If you open it in textEdit you will see 'bit.ly/18gECvy ˇÿˇ‡JFIFHHˇ€Cˇ€Cˇ¿Vˇƒ' as the first line. (The url points to Rick Roll as a joke by the professor.)
So I start building my app to open the same 'card.raw' file. I'm doing initial checks to see the app print to the console the same "stuff" as when I open it with TextEdit. Instead of printing out I see when I open it with TextEdit (see the text above), it starts and continues printing out text that looks like this:
\377\304 'u\204\206\226\262\302\3227\205\246\266\342GSc\224\225\245\265\305\306\325\326Wgs\244\346(w\345\362\366\207\264\304ǃ\223\227\2678H\247\250\343\344\365\377\304
Now I have no idea what the '\' and numbers are called (what do I search for to read more?), why it's printing that instead of the characters (unicode?) I see when I open in TextEdit, or if I can convert this output to hex or unicode.
My code is:
#include <stdio.h>
#include <string.h>
#include <limits.h>
int main(int argc, const char * argv[]) {
FILE* file;
file = fopen("/Users/jamesgoldstein/CS50/CS50Week4/CS50Recovery/CS50Recovery/CS50Recovery/card.raw", "r");
char output[LINE_MAX];
if (file != NULL)
{
for (int i = 1; fgets(output, LINE_MAX, file) != NULL; i++)
{
printf("%s\n", output);
}
}
fclose(file);
return 0;
}
UPDATED & SIMPLIFIED CODE USING fread()
#include <stdio.h>
#include <string.h>
int main(int argc, const char * argv[]) {
FILE* fp = fopen("/Users/jamesgoldstein/CS50/CS50Week4/CS50Recovery/CS50Recovery/CS50Recovery/card.raw", "rb");
char output[256];
if (fp == NULL)
{
printf("Bad input\n");
return 1;
}
for (int i = 1; fread(output, sizeof(output), 1, fp) != NULL; i++)
{
printf("%s\n", output);
}
fclose(fp);
return 0;
}
Output is partially correct (here's a snippet of the beginning):
bit.ly/18gECvy
\377\330\377\340
\221\241\26145\301\321\341 "#&23DE\3616BFRTUe\202CVbdfrtv\222\242
'u\204\206\226\262\302\3227\205\246\266\342GSc\224\225\245\265\305\306\325\326Wgs\244\346(w\345\362\366\207\264\304ǃ\223\227\2678H\247\250\343\344\365\377\304
=\311\345\264\352\354 7\222\315\306\324+\342\364\273\274\205$z\262\313g-\343wl\306\375My:}\242o\210\377
3(\266l\356\307T饢"2\377
\267\212ǑP\2218 \344
Actual card.raw file snippet of beginning
bit.ly/18gECvy ˇÿˇ‡JFIFHHˇ€Cˇ€Cˇ¿Vˇƒ
ˇƒÖ
!1AQa$%qÅë°±45¡—· "#&23DEÒ6BFRTUeÇCVbdfrtví¢
I think you should open the .raw file in the mode "rb".
Then use fread()
From the presence of the string "JFIF" in the first line of the file card.raw ("bit.ly/18gECvy ˇÿˇ‡JFIFHHˇ€Cˇ€Cˇ¿Vˇƒ") it seems like card.raw is a JPEG image format file that had the bit.ly URL inserted at its beginning.
You are going to see weird/special characters in this case because it is not a usual text file at all.
Also, as davmac pointed out, the way you are using fgets isn't appropriate even if you were dealing with an actual text file. When dealing with plain text files in C, the best way is to read the entire file at once instead of line by line, assuming sufficient memory is available:
size_t f_len, f_actualread;
char *buffer = NULL;
fseek(file, 0, SEEK_END)
f_len = ftell(fp);
rewind(fp);
buffer = malloc(f_len + 1);
if(buffer == NULL)
{
puts("malloc failed");
return;
}
f_actualread = fread(buffer, 1, f_len, file);
buffer[f_actualread] = 0;
printf("%s\n", output);
free(buffer);
buffer = NULL;
This way, you don't need to worry about line lengths or anything like that.
You should probably use fread rather than fgets, since the latter is really designed for reading text files, and this is clearly not a text file.
Your updated code in fact does have the very problem I originally wrote about (but have since retracted), since you are now using fread rather than fgets:
for (int i = 1; fread(output, sizeof(output), 1, fp) != NULL; i++)
{
printf("%s\n", output);
}
I.e. you are printing the output buffer as if it were a null-terminated string, when in fact it is not. Better to use fwrite to STDOUT.
However, I think the essence of the problem here is trying to display arbitrary bytes (which don't actually represent a character string) to the terminal. The terminal may interpret some byte sequences as commands which affect what you see. Also, textEdit may determine that the file is in some character encoding and decode characters accordingly.
Now I have no idea what the '\' and numbers are called (what do I search for to read more?)
They look like octal escape sequences to me.
why it's printing that instead of the characters (unicode?)
It's nothing to do with unicode. Maybe it's your terminal emulator deciding that those characters are unprintable, and so replacing them with an escape sequence.
In short, I think that your method (comparing visually what you see in a text editor with what you see on the terminal) is flawed. The code you have to read from the file looks correct; I'd suggest proceeding with the exercise and checking results then, or if you really want to be sure, look at the file using a hex editor, and have your program output the byte values it reads (as numbers) - and compare those with what you see in the hex editor.

Can not open files with c

Im supposed to write a program that opens an excel file, reads the numbers on the file, multiplies them by 9.8 and the shows the answer in another excel gile.
I wrote this, and I did not get any errors in the compiler, but when I run it, it does not open any files. How do I make it open the files?
#include <stdio.h>
int main() {
FILE *archivo;
FILE *archivoSalida;
int masa;
float peso;
archivo = fopen("C:/Users/nacho/Documents/UNAM/Informatica/proyecto/archivoEntrada.txt", "r");
archivoSalida = fopen("C:/Users/nacho/Documents/UNAM/Informatica/proyecto/archivoSalida.txt", "r");
if (archivo != NULL)
{
printf("The file was opened succesully");
while (fscanf(archivo,"%d", &masa)!= EOF)
{
peso=masa*9.81;
fprintf(archivoSalida, "%f\n", peso);
}
}
else
{
printf ("Error");
}
fclose(archivo);
fclose(archivoSalida);
return 0;
}
You'll want to fopen the output file ("archivoSalida") with mode "w" (for write) instead of "r" (for read). See e.g. http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html.
You do check if the input file could be opened (if (archivo != NULL)). Why don't you do the same for the output file?
Upon an error, you should output which error occured from errno, e.g. via perror(...). That should help in finding the actual problem.
Your file denominated by archivoSalida is opened in read mode ('r').
You should also check the return codes of read/writes functions to be sure everything happen as wanted.
The file names look Windows-ish. Is it possible that all of the forward slashes (/) that you have in both file names should really be back slashes (\)?

fopen() memory leak after few hundred runs

I'm writing an application which should read some data at some level from a file. When I run the code (including fopen() and fclose())for more than a few hundred times, I got the error message (which I know it means that it cannot open the file):
Debug Assertaion Failed!
Program:
D:\blahblah
file: f:\dd\vctools\crt\crtwin32\stdio\fgets.c
Line: 57
Expression: (string!=NULL)
Can you please help me to understand why it should break after more than three hundered time?
Func_Main(char * filePath, ...){
for(int i=0;i<1000;i++){
Func_1(filePath);
....
}
....
}
Func_1(char* filepath){
char buffer[1024];
FILE * file= NULL;
file = fopen(filepath, "r");
while(fgets(buffer, sizeof(buffer), file)){
\\ do something
}
fclose(file);
}
You should check the return value of fopen. It will return a null pointer if opening the file failed.
And it will fail because \ is an escape character , use \\ in the filename string.
Problems I see:
You never use the argument passed to Func_1. You use a hard coded path instead.
The hard coded path has an error. You are not escaping the backslash in the hard coded path . It should be:
file = fopen("c:\\blahblah.txt", "r");
^^
You are not checking whether fopen was successful. You are assuming it was successful. Use:
file = fopen("c:\\blahblah.txt", "r");
if ( file == NULL )
{
// Deal with the error.
perror("Unable to open the file");
}
Update, after the OP edited the question
The first two points can be ignored. The last point still needs to be considered.
file = fopen(filepath, "r");
if ( file == NULL )
{
// Deal with the error.
perror("Unable to open the file");
}

Resources