I started learning C programming language and I wrote popular "Hello world!" program.
but when I try to execute it, this appears
Traceback (most recent call last):
File "C:\Users\asebo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\asebo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "c:\Users\asebo\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
cli.main()
File "c:\Users\asebo\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
run()
File "c:\Users\asebo\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
File "C:\Users\asebo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 267, in run_path
code, fname = _get_code_from_file(run_name, path_name)
File "C:\Users\asebo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 242, in _get_code_from_file
code = compile(f.read(), fname, 'exec')
File "c:\Users\asebo\OneDrive\Počítač\codeblock\c.c", line 2
int main() {
^
SyntaxError: invalid syntax
And here is my full code:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
P.S: I am using VS code IDE
Looks like you've got a few Python paths in your compilation output, why?
If I were you, I'd just create a C++ console app. in VS, write it and save it as C code, give it a file name, i.e., "fileName.c" and Debug it from the VS File Menu.
Related
I understand ASCII 13 is \r and ASCII 10 is \n for Windows.
I was expecting to see A only in txt file, but I see two new lines before 'A' gets printed in the line 3. when I run the following code:
#include <stdio.h>
int main(void)
{
FILE* fp;
int ary[3] = {13, 13, 65};
int i, res;
fp = fopen("a.txt", "wb");
for (i = 0; i < 3 ; i++)
{
fputc(ary[i], fp);
}
fclose(fp);
return 0;
}
Where are the two new lines coming from?
I tried with different sets of array like {65, 13, 13, 65} or {65, 13, 10, 65}.
I was expecting to see one A in line 1 when ary is {65, 13, 13, 65}, but I get A in line 1, empty space in line 2 and another A in line3..
The program creates/overwrites a file. Because of the b, the file will contain exactly the values written to it: 13,13,65.
The above describes what the program does. But your question doesn't appear to be about what the program does; it appears to be about what Notepad does.
Windows line endings consist of the two-byte sequence 13,10. The CR characters are found outside of a line ending, so we're not dealing with a plain text file, but one that contains control characters. This means that how a text file editor will display this file can vary.
A carriage return is a move of the cursor to the home column, which can be used to overwrite characters on some terminals or overprint characters on some printers. But there's no reason for Notepad to act like a terminal or printer. It does not have to emulate a carriage return when it encounters a 13. According to what you say, it interprets the partial line ending as a line ending, which is perfectly acceptable.
When I wanted to use fopen() to read a file in Debugging, fopen() always return NULL and I can't locate the error after trying:
I just run the code, and fopen() works well, getting what I want. (but failed in debugging)
I am sured that the file (hello.txt) exists
I write a simple code like:
#include<stdio.h>
int main()
{
FILE *fp;
char str[50];
fp = fopen("F:\\notes\\assign\\bonus\\hello.txt","r"); //this line
fgets(str, 50, fp);
printf("%s", str);
return 0;
}
this code doesn't work too. I make a breakpoint at "this line" and watch how the FILE *fp changes.
before:
fp: 0x00007ff663b31110 {hello.exe!void(* pre_cpp_initializer)()} {_Placeholder=0x00007ff663ac74a4 {hello.exe!pre_cpp_initialization(void)} }
after:
fp: 0x000001b7d6c3eb50 {_Placeholder=0x0000000000000000 }
you can see fopen() returns NULL;
I also tried freopen() and fopen_s(), but failed too.
For more information:
I use vscode. my compiler is "clang", my debugger is Windows VS so that I have to lauch vscode in Developer Cmd Prompt.
I would appreciate if anyone can help me. This disturbs me for a long time.
fp = fopen("F:\\notes\\assign\\bonus\\hello.txt","r"); //this line
A failure by fopen() (and many other standard library functions) will set errno to an error code indicating the error cause. You can turn that into a proper error message by adding code like this:
if ( fp == NULL )
{
perror( "Failed to open hello.txt" );
exit( 1 );
}
perror() will append a description of the error cause to the string you have given as argument, and print that to stderr. If you want to log the error message elsewhere, strerror() will write the error message to a string buffer.
I am trying to make a small program in C which reads in a file and calculates a CRC on the contents. I created the program in netbeans and in the IDE everything works. When I build the project with GCC and run the generated exe the program fails when reading in the file. The file is 1.3 Mb.
When reading in the file I eventually get the error "Internal error: TP_NUM_W_BUFS too small: 50"
My read code is fairly simple, it does very little line processing.
while (fgets(line, HEX_LINE_LENGTH, fp))
{
int len = strlen(line);
line[len] = line[len-1];
line[len-1] = '\r';
memcpy(&hex_lines[num_lines], line, HEX_LINE_LENGTH);
num_lines++;
printf("%s\n", line);
memset(line, 0, HEX_LINE_LENGTH);
}
I am seeing a new issue. When reading in my file via the netbeans IDE everything works fine. When I compile the program from a command line with
g++ main.c crc_calculator.c crc_calculator.h -o crc
I get a crc.exe file. Running this will read in my file but will report that twice as many lines have been read than actually exist.
First of all, this is my fist stack overflow question, so forgive me if I format this wrong.
I am a beginner at C, and I am up to a point in my book on File i/o. The following code, which is supposed to print the lines to test.txt, doesn't create a new txt file or... do anything.
I am running Code Blocks 16.01 on Windows. Is this code designed for another OS?
#include <stdio.h>
#include <stdlib.h>
main() {
FILE *fp;
fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
Ok, so removing the slash makes it work. In the original code, it is 'fopen("/tmp/test.txt", "W+");'
Shouldn't this create the file in folder tmp?
Try removing the front-slash from the file name. You seem to be doing everything properly, the slash might be the problem. If not, let us know.
Edit: When I wrote my comment your fopen used "/test.txt" and not "/temp/test.txt", do you have the "temp" folder created in the directory the application is running from? If not, try creating it. Or remove it altogether and try creating the text file within the directory the application is running from.
Use double // in windows for navigate through directory.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
/*
file path in windows should be like this= C:\\users\\r.maurya\\Desktop\\Downloads\\file.txt
*/
fp = fopen("C:\\users\\r.maurya\\Desktop\\Downloads\\file.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
return 0;//Optional, On success of program
}
Trying to learn C. Want to read the first line of a text file, my code is:
#include <stdio.h>
int main()
{
FILE *in = fopen("test.txt", "rt");
// read the first line from the file
char buffer[100];
fgets(buffer, 20, in);
printf("first line of \"test.txt\": %s\n", buffer);
fclose(in);
return 0;
}
I'm doing this in xCode. I get a exc bad access error.
test.txt definitely exists. It has one line that says "this is a text file"
try this after fopen() call:
if(in == NULL){
printf("Can't read teste.txt because: %s.\n", strerror(errno));
return 1;
}
and add the headers:
#include <errno.h>
#include <string.h>
The code looks fine, so my guess is that the program is not run in the same working directory as the file. Try placing the file in, say, /tmp/test.txt and use absolute path in fopen.
You don't check if the FILE is NULL. It may not be opened for a several reasons.