@asrockw7
Not sure, but I think that binary files don't have '\n'. I wrote it like that for it to be more transparent.

I tried the following. It doesn't work for the last entry for some reason. Can anyone tell me why?

[CODE]
db=fopen("d.bin","rb"); if(!db) exit(1);
pgr=1;
fseek(db,0,SEEK_SET);

for(j=0;;j++){
pgr=fread(&structs[j],sizeof(struct record),1,db);
//printf("%s %lf\n",structs[j].naziv,structs[j].value);
if (pgr!=1) break;
}[/CODE]

Here I am again...

The question that I have is this: How do I sort an array of structures by two criteria?

One element of the array looks like this:
[CODE]struct record{
char name[32];
double value;};[/CODE]

The goal would be to sort the array by name and if the name is the same, by value.

For example, let's say the array is:

A 1
B 13
B 1
B 2
C 3
C 1

The result of sorting should be:

A 1
B 1
B 2
B 13
C 1
C 3

My code looks sort of like this:
[CODE]

include <stdio.h>
include <stdlib.h>
include <string.h>

struct record {
char name[32];
double value;
};

struct record structs[32];

int function_comparing_values(const void a, const void b)
{...
}

int function_comparing_names(const void a, const void b)
{...

if (returnvalue==0)
function_comparing_values; //THIS IS THE KEY PART RIGHT HERE
else // AND IT'S NOT CORRECT.
return returnvalue;
}

void function_printing_array(struct record *array, size_t len)
{...
}

void sort_structs_example(void)
{

{some functions to fill the array}

print_struct_array(structs, structs_len);

puts("*** Struct sorting (name)...");

qsort(structs, structs_len, sizeof(struct record), function_comparing_names);

print_struct_array(structs, structs_len);

}

int main(){
    sort_structs_example();    return 0;}[/CODE]

If you'd like I could upload the entire code, but I thought that it

would be more readable this way.

I am not quite sure what do you mean.

I could do this:

[CODE]
record test; //declaration of test which is type record

FILE* db=fopen("d.bin","rb");

fread(test.name,sizeof(test.name),1,db)[/CODE]

But from that point how do I put test.name inside an array of strings??

Tried something like this:
[CODE]
for(i=0;(not sure which would be the right condition);i++){

fread(test.name,sizeof(test.name),1,db)
name[i]=test.name;

}
[/CODE]

And it didn't work.

Well, hello.

I have a problem with reading data from binary files into arrays.

For example, let's say that we have d.bin file which contains

pairs of strings with max lenght of 32 chars and double values like this:

string12.334343
AS34.34
something3

where the string always takes up 32 bytes.

Basically, the binary file has a series of types record defined like this:

typedef struct{
char name[32];
double value;
}record;

The question is how would I fread the strings into one array and values into another so that

the namearray would contain (string AS something)

and the valuearray (12.334343 34.34 3) ??

[QUOTE]Then why haven't you completed it yet? [/QUOTE]

I meant no work for someone who knows what to do. And if I were that person, I wouldn't be asking for help, now would I? :)

And yes, I said that I didn't understand the example I was given before because strtod was used in a different situation.

Anyway, thanks for the help.

The following code does the trick. Is there a simpler way to do this(less strings, without strcat)?

Also, could you explain what does %.f actually mean? I mean, is usually used for pointers.

[CODE]#include<stdio.h>

include<string.h>
include<stdlib.h>

double a;
char c[101],d[101],e[1000],p[1];
main()
{
p[0]='.';
scanf("%100[^.].%s",c,d);
printf("%s.%s\n",c,d);

strcat(e,c);
strcat(e,p);
strcat(e,d);
size_t dec=strlen(d);
printf("%s\n",e);
a=atof(e);
printf("%.*f\n",dec,a);
return 0;
}
[/CODE]

I don't need that kind of an example because I know how to use atof. I think the question is really simple. There is no "work" here. I'm not requiring a 1000-line code.

And also, I asked why doesn't the following code work.

[CODE]#include<stdio.h>

include<string.h>

double a;
char c[101],d[101],e[1000],p[1];
main()
{
p[0]='.';
scanf("%100[^.].%s",c,d);
printf("%s.%s\n",c,d);

strcat(e,c);
strcat(e,p);
strcat(e,d);
printf("%s\n",e);
a=atof(e);
printf("%f\n",a);
return 0;
}
[/CODE]

Well, that is not even close. It turns string to float which was not my question. Output is supposed to be identical to input in terms of decimal spaces.

I tried this. It doesn't work.
[CODE]#include<stdio.h>

include<string.h>

double a;
char c[101],d[101],e[1000],p[1];
main()
{
p[0]='.';
scanf("%100[^.].%s",c,d);
printf("%s.%s\n",c,d);

strcat(e,c);
strcat(e,p);
strcat(e,d);
printf("%s\n",e);
a=strtod(e);
printf("%lf\n",a);
return 0;
}
[/CODE]

I don't really understand the example on the link. What's the division for? How would I use the function on one number?

Like this? But how do I convert 2 strings to double? Need I use strcat?

[CODE]#include<stdio.h>

char c[101],d[101];
main()
{
scanf("%100[^.].%s",c,d);
printf("%s.%s",c,d);

return 0;
}
[/CODE]

Hello.
The title pretty much says it all. I would like to know if and how you can print a number (type is double) with the same amount of decimal spaces as the one you entered.
If I enter 12.34 I would like the output to be 12.34 and not 12.340000000

Let's say that the program is this:

[CODE]

include<stdio.h>

double a;

main(){

scanf("%lf",&a);
printf("%lf\n", a);

return 0;
}
[/CODE]

First of all, I don't even know what do you mean by "format the code properly". I used tabs, enters and spaces as much as I can see. What is so improper about it? I could have answered if you had asked a question about the code which is precisely what you didn't do. I don't think I'm above rules but I'm not really aware of the fact that I have broken any. I asked you to create 2 .txt files of 6 numbers, which isn't exactly the twelve tasks of Hercules, because that is what I am supposed to do. And by the way, don't you think that your answers were a bit too generic, effortless and arrogant?

For the love of god, how difficult can it be? Can't you just run the program instead of complaining about formatting? I wrote that you'll need 2 .txt files and i gave an example of their content. I also wrote how to run the program. And as far as I can see the formatting is not bad at all. You have 5 cases all of which are separated by an empty line and inside those 5 blocks you have one command per line. I really don't see what the issue is. Unless you don't want to help and in that case, I don't understand the point of you posting in this thread.

Ok,

you have a.txt and b.txt which both contain two collumns: the first column is the value of a parameter.

the goal would be to use the values inside the files and add, subtract, multiply them and etc.

for example:
a.txt:
2 0.1
3 0.1
4 0.1

b.txt:
1 0.1
1 0.1
1 0.1

and in case a.txt + b.txt the result in output should be:

3 0.14
4 0.14
5 0.14

the first column is simply adding the values while the second is calculated from an expression derived from calculus, but really, the numerical values are not the issue here. So don't worry about the second column - it's not meant to be 0.2 as you can see in the code.

I apologize for the first code, I forgot to change the names of the variables into something english-like.

Just go through the first if to understand what the program is about. Else ifs are just a variation.
However, while I get good results for + and - I don't get anything printed out in case of other operations.

Why am I not getting any output for */^ ?

[CODE]

include <stdio.h>
include <math.h>
include <stdlib.h>

main(int argc, char* argv)
{
FILE
fa = fopen(argv[1],"r"); if(!fa)exit(0);
FILE* fb = fopen(argv[3],"r"); if(!fb)exit(0);
double a,b,errora,errorb,res,reserror;

if( argv[2][0] == '+' ){
while((fscanf(fa,"%lf %lf\n", &a, &errora))>0){
fscanf(fb,"%lf %lf\n", &b, &errorb);
res=a+b;
reserror=sqrt(pow(errora,2) + pow(errorb,2));
printf("%8.2lf %8.2lf\n",res,reserror);}
}

else if( argv[2][0] == '-' ){
while((fscanf(fa,"%lf %lf\n", &a, &errora))>0){
fscanf(fb,"%lf %lf\n", &b, &errorb);
res=a-b;
prez=sqrt(pow(errora,2) ...

So, there's no one to help?

Hi once again.

As the title implies, the goal of the following code is to create calculator for operations +,-,*,/ and ^. However, the operations are to be executed on values in the files I arbitrarily named a.txt and b.txt.
a.txt contains following data:
a1 era1
a2 era2
.
.
.
an eran

and b.txt:
b1 erb1
b2 erb2
.
.
.
bn erbn

era and erb are belonging errors in measurement for a and b (in case you are wondering)

The result should be in the standard output and look like:

a1(operation)b1 resultingerror1(whose value depends on operation, but nevermind)
.
.
.
an(-||-)bn resultingerrorn(-||-)

If you don't get it from the description just create two .txt files, enter some values and run the program in terminal like this

gcc name.c -lm -ocalc
./calc a.txt + b.txt

And so... The following code works perfectly for + and -, but for some reason in case of other operations the program prints absolutely nothing.
Why is that? How would I correct it?

[CODE]

include <stdio.h>
include <math.h>
include <stdlib.h>

main(int argc, char* argv)
{
FILE
fa = fopen(argv[1],"r"); if(!fa)exit(0);
FILE* fb = fopen(argv[3],"r"); if(!fb)exit(0);
double a,b,pa,pb,rez,prez;

if( argv[2][0] == '+' ){
while((fscanf(fa,"%lf %lf\n", &a, &pa))>0){
fscanf(fb,"%lf %lf\n", &b, &pb);
rez=a+b;
prez=sqrt(pow(pa,2) + pow(pb,2));
printf("%8.2lf %8.2lf\n",rez,prez);}
}

else if( argv[2][0] == '-' ){
while((fscanf(fa,"%lf %lf\n", &a, &pa))>0){
fscanf(fb,"%lf %lf\n", &b, &pb);
rez=a-b;
prez=sqrt(pow(pa,2) + pow(pb,2));
printf("%8.2lf %8.2lf\n",rez,prez);}
}

else if( argv[2][0] == '' ){
while((fscanf(fa,"%lf %lf\n", &a, &pa))>0){
fscanf(fb,"%lf %lf\n", &b, &pb);
rez=a
b; ...

I thought that the program's instant "assumption" would be that the number is positive (since I didn't do anything to make it "think") and in the case it's negative i tried to count '-' signs in the string (see below). The program converts the number properly, but for the sign.

Also there is the problem of potential - after 'e' which shouldn't change the sign of value in float, but the value itself.

[CODE]flag2=count(s,'-');
if(flag2!=0){
num=num*(-1);}[/CODE]

Hi. I have trouble with getting the following program to convert string to float type. What i need to do is this:

12.345e3 = 12345
12e-2=0.12
-12.34=-12.34
-12.34e3=-12340
-12.34e-2=-0.1234

e: = x 10^

I got it to do the basics. However, It doesn't work in case of negative numbers or exponents. While i completely understand it for exponents, i can't say the same for negative numbers. Also, I am pretty sure the code can be optimised. I just don't know how to do it.

[CODE]#include <stdio.h>

include <math.h>
define SIZE 1024

int count(char S[SIZE],char c){
{
int i=0, count=0 ;
for( ; S[i] != 0 ; ++i )
if( S[i] == c ) ++count ;
return count ;
}}

float atof( char s )
{
float num = 0.0;
float kon = 0.0;
int flag,flag2,i;
float exp;
while (
s) {
if (s >= '0' && s <= '9') {
num = 10.0 num + (float)(s - '0');
kon = 10.0;
}
else if (
s == '.') kon = 1.0;
else if(s == 'e'){
break;}
s++;
}
for(i=1;
(s+i);i++)
exp=exp10+((s+i)-'0');

flag=count(s,'e');
if(flag!=0){
num = num / (kon == 0.0 ? 1.0 : kon);
num=num*pow(10,exp);}
else{
num = num / (kon == 0.0 ? 1.0 : kon);}

flag2=count(s,'-');
if(flag2!=0){
num=num*(-1);}
return num;
}
main()
{
float f;
char a[SIZE];
scanf( " %s", a );
f = atof(a);
printf( "atof(\"%s\")=%f\n", a, f );
}
[/CODE]

Thank you both very much.

Anyone?

WaltP commented: We will get to it when we get to it. Bumping your post is rude -4

Wow, man, you are good. Can you tell me why use size_t and not int for orig_len and rep_len? I tried it and it works with int as well. And what would i need to do so that the program would do the same if return type for function replace_str were void and not char?

Hello to all. I have a problem with replacing a part of a string with another string.
The following code does not work. However, if you remove void in front of replace_str, in Code Blocks i am able to run it (WHY???) despite the fact that i still get a warning:

"argument 1 of 'puts' makes pointer from integer without a cast"

Also, this "program" only replaces the first occurance of s2 in s1 and it should replace all.

Plus, i would like to know the correct sintax to print res using printf (the comment in code does not do it).

Every part of code besides

[CODE]void replace_str(char res, char str, char orig, char rep)[/CODE]

can be changed (according to assingment).

Any sort of help would be much appreciated.

[CODE]#include<stdio.h>

include<string.h>
include<stdlib.h>
define SIZE 1024

void replace_str(char res, char str, char orig, char rep)
{
char buffer[4096];
int i;
char *p;

if(!(p=strstr(str, orig))){
for(i=*p;i<strlen(orig);i++)
res[i]=str[i];
}
else{
strncpy(buffer, str, p-str);
buffer[p-str] = '\0';

sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

res=buffer;
}}

main()
{char res[SIZE],s1[SIZE],s2[SIZE],s3[SIZE];
scanf(" %s",s1);
scanf(" %s",s2);
scanf(" %s",s3);
puts(replace_str(res,s1, s2, s3));
//replace_str(res,s1, s2, s3);
//printf(" %s\n",res);
return 0;
}
[/CODE]