problem about stdio-Collection of common programming errors
Claudio
c embedded avr stdio avr-gcc
I’m developing a C application using avr-libc on an AVR Atmega328P microcontroller. Since I don’t have an ICE debugger for it, I followed this instructions and this tutorial for making the stdio.h functions such as printf able to use the hardware UART as stdout.That works, I can see the output on a PC terminal connected to my target board, but the strange thing is: when I have only one printf on main but before the main loop something is causing the processor to reset, while if I have a printf o
Joe
c stdio
I wrote a simple C program, which takes a .txt file and replaces all spaces with hyphens. However, the program enters an infinite loop and the result is endless array of hyphens. This is the input file:a b c d e fThis is the file after the process crashes:a—————————————————————————- —————————————-… (continues thousands of times)… I guess the reason in unexpected behavior of fread(), fwrite() and fseek(), or my misun
sky
visual-studio stdio
I could find stdio.h header file easily through search in Windows Explorer but couldn’t find its implementation file like stdio.lib(?). Where can I find it?Additionally, I can’t find Windows.h through search in Windows Explorer although I can compile source code with Windows.h included. So weird. :-p Is there anybody to explain about this?
Spontifixus
c arrays std stdio dynamic-arrays
Using the same machine and IDE as reffered in my other question (third paragraph at Problems in code or my IDE/comp is bugged?)I try to run this code:#include <stdio.h> #define n 3 int main() {int i;float values[n],sumval,svmean,tmp;for(i=0;i<n;++i){scanf(“%f”,&tmp);values[i]=tmp;sumval = sumval + values[i];}svmean = sumval/n;printf(“%f \n”,svmean);return(0); }The above code is supposed to run this formula That means that it has to add some values and divide the result by their tota
Mat
c windows pipes stdio
I’m trying to parse data from stdin in binary mode under Win32. The first thing my code does is to check for a 4byte header at the beginning:int riff_header; fread(&riff_header, sizeof(riff_header), 1, ifp); // ‘RIFF’ = little-endian if (riff_header != 0x46464952) {fprintf(stderr, “wav2msu: Incorrect header: Invalid format or endianness\n”);fprintf(stderr, ” Value was: 0x%x\n”, riff_header);return -1; }stdin has been switched to binary mode before reading from it:if (*argv[argc-1] =
Jagger
c++ stdio file-rename
I was just wandering if stdio::rename() function call is fully synchronous. So is the file immediately after return from the function call available under new name or it might take some time (some milliseconds) until this happens? I am investigating an irritating timing bug and suspect that the latter case happens.My software runs on Win2k3 server machine.
Rohit Banga
c linux stdio fifo
i have a closed source program that prints output to standard output. i need to parse the output. so i redirect the output to a fifo (from which i can read in the parent process that forks and execs the binary) using dup2 and then exec the program. the problem is that the fprintf calls in the file become buffered because it is now writing to a file.i tried calling setvbuf with _IONBF on stdout before calling exec. but the problem still exists.why does setvbuf not help in my case?how can i force
Thomas Padron-McCarthy
c++ c stdio buffering fprintf
I’m using fprintf in the following way. Everything seems to be ok but fprintf doesn’t print to my file at all!fprintf(pFile, “%s\n”, “print”);Something that is strange is that fprintf returns OK. it returns 6 in the above code, but not printing to file!The file is created successfully but is empty.changing it to printf is printing and OK too.
walkytalky
c++ objective-c ios fopen stdio
I am puzzled by a crash I keep getting due to an error at this section of code:FILE *fid200;fid200 = fopen ( “Length200Vector.txt” , “w” );if (fid200 == NULL)perror(“Error opening Length200Vector.txt”);for (int n = 0; n<200; n++) {if (n == 0) {fprintf (fid200, “%f”, self.avgFeatureVect[0][n]);}else {fprintf (fid200, “, %f”, self.avgFeatureVect[0][n]);}}fprintf (fid200, “\n”);fclose(fid200);The error is: Error opening Length200Vector.txt: Operation not permitted.The file is residing in my Reso
David Dean
c stdio
I’m working on some code that generates a lot of ignoring return value of ‘size_t fwrite(const void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_resultwarnings when compiled with g++, and I’m wondering about the best programming pattern to actually record and handle the return value of a large number of separate sequential fwrites (i.e. not the same fwrite in a loop)Let’s say that the code looks like this at the moment:fwrite (&blah, sizeof (blah), 1, fp); // … more code
Mike
c arrays stdio
I have written a simple program in C:#include <stdio.h>main(){int a[20], b[20];int n, i;printf(“Enter a number: “);scanf(“%d”, &n);for(int j=0; j<n; j++){printf(“Enter a number for a[%d]: “, j);scanf(“%d”, a[j]);printf(“\n”);} }This code compiles but while running when n is greater than 2 and when input a second number in to the array an crash occurred.I don’t underestant why it crashed, please explain it to me.
icedwater
c++ io iostream stdio
When I search on the internet for the difference between these two libraries, everyone says that <iostream> is the standard I/O library of C++ and <cstdio> is for C. My professor says that cin>> and cout<< are not good functions and if we use cin>> many times our application will definitely crash. He also says that stdio provides nearly 3 times faster input and output than iostream. However, I prefer using iostream because it is more convenient, and also I don’t kno
user35443
c file file-io rewrite stdio
I have small problem with rewriting first 2,5KB of file. My code should read 2KB and 512 bytes to dynamically allocated memory and then rewrite specific bytes of much larger file.f = fopen(argv[2], “rb”); if(f==NULL)printf(“File doesn’t exist!”); fseek(f, 0, SEEK_SET); data = calloc(2*1024+512, 1); fread(data, 1, 2*1024+512, f); fclose(f);f = fopen(argv[1], “ab”); if(f==NULL)printf(“File doesn’t exist!”); fseek(f, 0, SEEK_SET); fwrite(data, 1, 446, f); fseek(f, 512, SEEK_SET); fwrite(((char*)da
PJ.Hades
c stdio buffering
In the book Advanced Programming in the UNIX Environments (2nd edition), the author wrote in Section 5.5 (stream operations of the standard I/O library) that:When a file is opened for reading and writing (the plus sign in the type), the following restrictions apply. Output cannot be directly followed by input without an intervening fflush, fseek, fsetpos, or rewind. Input cannot be directly followed by output without an intervening fseek, fsetpos, or rewind, or an input operation that encounte
dmckee
c alias stdio restrict-qualifier
tl;dr Can asprintf be used naively for concatenation without invoking a temporary pointer?The function asprintf introduced by GNU and adopted in several other clib implementations is a tempting solution for arbitrary concatenation in c using a scheme likeint i=0; char *str = strdup(argv[i]); while (argv[++i]) {asprintf(&str,”%s %s”,argv[i],str); // <=== This line } asprintf(&str,”%s\n”,str);When wrapped in a main and the necessary includes, this runs fine for me. But…Is it leaking
Dave
c performance locking stdio mingw32
I had been struggling for weeks with a poor-performing translator I had written. On the following simple bechmark#include<stdio.h>int main() {int x;char buf[2048];FILE *test = fopen(“test.out”, “wb”);setvbuf(test, buf, _IOFBF, siseof buf);for(x=0;x<1024*1024; x++)fprintf(test, “%04d”, x);fclose(test);return 0 }we see the following resultbash-3.1$ gcc -O2 -static test.c -o test bash-3.1$ time ./testreal 0m0.334s user 0m0.015s sys 0m0.016sAs you can see, the moment the “-std=c99
Bart
c linux buffer stdio
(note: This question is for some schoolwork I’m doing. I don’t need to know how to implement this, just need to know if it’s possible and how can I learn more about it)I’d like to use a user level buffer for a file using only a file descriptor and not using a FILE*. I know stdio.h has, at least, 1 fifo user level buffer for all *printf() and *scanf() functions.My question:I was asked to do a system that uses the write() linux function. That function is a system call. The objective here is to com
Jonathan Wakely
c malloc stdio fseek
On my project, I need to copy the shared file into a directory which called share. My idea is to copy the contains of this file use fgets and fputs:FILE *fp; int size; char *fileBufffseek(fp,0,SEEK_END ); size=ftell(fp); printf(“Size of %s: %d bytes.\n”,path,size); // print correct size fileBuff=malloc(size); // mallocate the file buffer printf(“\nsize of file buffer is %d”,sizeof(fileBuff)); //always print 4!! while(!feof(fp)){fgets(fileBuff,size,fp); // put into file buffer} printf(“\nsize of
Greg Rogers
c stdio
It seems as though the following calls do what you’d expect (close the stream and not allow any further input – anything waiting for input on the stream returns error), but is it guaranteed to be correct across all compilers/platforms?close(fileno(stdin)); fclose(stdin);
user7116
c++ stdio
Possible Duplicate:Why does std::cout output disappear completely after NULL is sent to it It seems if you try:std::cout << NULL << endl;std::cout << “hell” << endl;it print out nothing and C++ IO stops working for all subsequent outputs.but it works fine in C stdio:printf(“%s\n”, NULL);printf(“%s\n”, “hell”); (null)hellIs there any good reason why C++ IO can’t do the same thing?(edited in response to comments) alright, to make it clear, NULL does have a type, say const
user1058795
c sprintf stdio
If I runfilename=”heat.dat”; prtdat(u_x_length, u_y_length, u[iz],filename);it works fine. If I change the first line toprintf(“%d”,sprintf(filename,”heat.dat”));the output is 8, and then my program crashes. Why?? My actual aim is to use sprintf(filename,”heat%dof%d.dat”,rank,numtasks).If you need the prtdat routine, here goes:void prtdat(int u_x_length, int u_y_length, float *u, char *fnam) { int ix, iy; FILE *fp;fp = fopen(fnam, “w”); for (iy = 0; iy < u_y_length; iy++) for (ix = 0; ix <
Carl Norum
c pointers stdio
{char *a, *b;printf(“%lx\n”,(b-a)); }Usually works, in fact, I can’t imagine it giving a warning or failing on a 32-bit or 64-bit machine. But is that the proper thing to do for ANSI C and size awareness? I’d like this code to work on every platform possible, including non-Unixes and embedded systems.
larsmans
c stdin stdio
I have function in C that assumes stdin is open. I want to add an assertion in front of it to make sure stdin is not closed by anyone. How can I check that stdin isn’t closed by anyone?assert(is_open(stdin));
Vilhelm Gray
c file-io c99 stdio ftell
On a 32-bit system, what does ftell return if the current position indicator of a file opened in binary mode is past the 2GB point? In the C99 standard, is this undefined behavior since ftell must return a long int (maximum value being 2**31-1)?
Natan Yellin
c printf glibc stdio libc
According to section 4.9.6.1 of the C89 draft, %d is a character that specifies the type of conversion to be applied.The word conversion implies, in my opinion, that printf(“%d”, 1.0) is defined.Please confirm or refute this.
Eric
.net process stdout stdio
I’m using the following codeSystem::Diagnostics::Process^ p = gcnew System::Diagnostics::Process(); p->StartInfo->FileName = “tnccmd.exe”; p->StartInfo->UseShellExecute = false; p->StartInfo->RedirectStandardInput = true; p->StartInfo->RedirectStandardOutput = true; p->Start(); System::IO::StreamWriter^ tnc_stdin = p->StandardInput; System::IO::StreamReader^ tnc_stdout = p->StandardOutput;tnc_stdin->WriteLine(“connect i 127.0.0.1”); String^ prg_output = tnc_
caitriona
makefile stdio
I’m trying to use the sprintf() function. Therefore I have to include the stdio.h in my C project. If I compile the project without including the stdio.h in my makefile, the compiler generates the error that sprintf() is a unknown function. Including the stdio.h to the makefile generates the error that there is “no rule to make target.” The makefile template gives the options as follows:NAME = testCC = arm-none-eabi-gcc LD = arm-none-eabi-ld -v AR = arm-none-eabi-ar AS = ar
Oddant
c include header-files stdio
I’m not totally sure but this looks wrong:I have a header file named fraction.h in which I store a fraction structure and the methods to handle it, one method is used to write a fraction in a file and in the signature of this function one argument is a FILE pointer.fraction.h:… const Fraction * fraction_fwrite(const Fraction * f, FILE * file); …fraction.c:#include <stdio.h> #include “fraction.h” …Now when I try to compile a program that uses a fraction, I get an error,here is what I
Arheisel
mysql c stdin fgets stdio
This is my code:#include <mysql.h> #include <stdio.h> #include <stdlib.h> #include <string.h>char *server = “nope”;char *user = “nope”;char *password = “nope”;char *database = “nope”;unsigned int port = 0;int cprint(char *text){//Imprime como encabezado el numero de productos sin stock sumado al argumentoMYSQL *conn;MYSQL_RES *res;my_ulonglong num;conn = mysql_init(NULL);/* Connect to database */if (!mysql_real_connect(conn, server,user, password, database, port, NULL, 0)
Mad Rapper X
c stdio
I want to process the contents of a config file. The config file could be any size. I am getting a Bus Error, after the program hangs, when I run the following code:FILE *fp; struct stat st; char *buffer;fp = fopen(CONFIG_FILE, “r”); if (fp == NULL) {// error handling and cleanup omitted for brevity }fstat(fileno(fp), &st); fread(buffer, sizeof(char), st.st_size, fp); fprintf(stderr, “%s\n”, *buffer); fclose(fp);I have read that a bus error can be caused by a buffer overflow. I am pretty
Web site is in building