Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Lets say that I have file which contains the data
Data1 "X1 Y1 Z1"
Data2 "X2 Y2 Z2"
Data3 "X3 Y3 Z3"
Generally, how would I scan the file and make my program count "X1 Y1 Z1" as a single token?
#include <stdio.h>
int main(){
FILE *fp = fopen("data.txt", "r");
char data_name[16];
char data_string[32];
while(2==fscanf(fp, "%15s \"%31[^\"]\"", data_name, data_string)){
printf("%s, %s\n", data_name, data_string);
}
fclose(fp);
return 0;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How can I view a number of user groups?
I mean the implementation in C.
I wanted to use the getgrouplist() function, but I want it to take the number of groups automatically.
Here's an off the cuff program which seems to work on my macOS 10.14.1 system, which seems to be quite behind the times:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int res = 0;
int ng = 100;
int gs[ng];
res = getgrouplist("hacksaw", 20, gs, &ng);
printf("num_grps: %d\nres: %d\n", ng, res);
}
Here's one which works on Ubuntu 16.04:
#include <stdio.h>
#include <grp.h>
int main(void)
{
int res = 0;
int ng = 100;
gid_t gs[ng];
res = getgrouplist("hacksaw", 20, gs, &ng);
printf("num_grps: %d\nres: %d\n", ng, res);
}
The size 100 was chosen arbitrarily to provide maybe enough space.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Errors:
Test isPower2(0[0x0]) failed.
Gives 1[0x1]. Should be 0[0x0]
Code:
int isPower2(int x) {
int nonNega = (x>>31);
int result = !((x & (x-1)) ^ nonNega);
return result;
}
Your isPower2(0) returns true (1) but 0 is not a power of 2. So the expected result would be false (0).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
A for loop to be exact. I've tried using srand() to seed the loop but it doesn't seem to be working. Could someone clarify the seeding part? Anyways, could someone point out my errors?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int a,b,c,d,loop1=0,loop2=1,count=0;
srand(1);
while(loop1!=1)
{
a=rand()%10;
b=rand()%10;
c=rand()%10;
d=rand()%10;
printf("%d %d %d %d\n",a,b,c,d);
count+=1;
if (count>3)
{
break;
}
}
return 0;
}
The output should look like this:
1 2 3 4
1 2 3 4
1 2 3 4
You need to set the seed at the beginning of the loop if you want to have the same sequence in each iteration:
while(loop1!=1) {
srand(1);
a=rand()%10;
b=rand()%10;
c=rand()%10;
d=rand()%10;
printf("%d %d %d %d\n",a,b,c,d);
count+=1;
if (count>3) {
break;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a list of files. For each of those files, I need to find out if the starting two chars are - '#!'. How do I do this?
Load the two numbers using fgetc() (as #WilliamPursell suggested) and then compare them:
int i1, i2;
FILE *file;
file = fopen("yourfile", "rb");
if (file == NULL) {
printf("Error: failed to open file");
return 1;
}
i1 = fgetc(file);
i2 = fgetc(file);
// 23h...#, 21h...!
if (i1 == 0x23 && i2 == 0x21) {
// magic number
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to read d characters of a file and split that into two 16bits say for example:
text[1]=0x7469206564616d20;
text[2]=0x7469206564616d20;
How to do that?
INSTEAD OF TEXT[0] AND TEXT[1] PREDEFINED VALUES I WANT IT 2 READ D CHARACTERS FROM A FILE AND SPLIT IT IN2 THAT FORM
You probably want this:
u64 text[2] ;
FILE *input = fopen("myfile.data", "r") ;
if (input == NULL)
{
printf ("Unable to open file\n") ;
return 1 ;
}
while (!feof(input))
{
int charsread ;
charsread = fread(text, 16, 1, input) ; // read at most 16 bytes into the text array
// process your text array here
// charsread contains the number of characters actually read
// this can be less than 16 if the total file length is not a
// multiple of 16. You mus deal with that.
}
fclose(input) ;