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 6 years ago.
Improve this question
SOLVED BELOW
I'm writing an IP forwarding program and I'm trying to read the header data.
I have structs for different lines such as this for the first line:
struct line1 {
char a; //version
char b; //header length
unsigned short c; //datagram length
};
The different data types are dependent on the length of the data field.
I have variable initialization:
struct line1 l1 = {};
FILE *ip_packets, *routing_table;
My professor showed a simple read function that was something like read(ip_packets, 4, l1) (4 Bytes) that automatically put the data into the struct fields. I have searched around the web and haven't found a simple method like this. What read function am I looking for?
I've tried fscanf in this way:
if (fscanf(ip_packets, "%c %c %hu", &l1.a, &l1.b, &l1.c)){
printf("%c\n", l1.a);
printf("%c\n", l1.b);
printf("%hu\n", l1.c);
}
I've also tried syntaxt %c,%c,%hu or %c/%c/%hu
but that just prints:
Kendalls-Mac-mini:Programming 2 kendallweihe$ ./ip_read
E
0
SOLUTION
Turns out I was reading it in correctly, but I needed to print the integer value. My testing verification is in terms of integers. Easy enough.
BETTER SOLUTION
fread(&l1, 4, 1, ip_packets);
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 3 days ago.
Improve this question
I am doing the CS50 problem set 2 wordle and I am stuck with the TODO: 5.
I have two words of the same length: the "guess" from the user and the "word", which they try to guess.
I am supposed to check if some letters of the "guess" are in the actual "word" and if they are in the correct spot.
I tried to make an array of characters which is basically the word itself in order to compare each letter of the guess with the actual word but everything I find is just looks like that:
char myString[] = "This is some text";
But the problem is that the words are saved as strings and I cannot do this:
char myString[] = guess;
It would be great if someone could help me with that problem.
char myString[] = "%s", guess;
but that does not work!
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
Im getting 10 bytes of data in an char array like which contains hex value
Data1[0] = 0x00,Data1[1] = 0x00,Data1[0] = 0x9 Data1[2]=0x01and so on...
Now I want to get this different array bytes into single long variable . Like
Long_var = 091...
How can do it any method can be accepted.
Sorry, i forgot to mention, i want to do this in 8051 code
There are generally two ways to do type punning in C, both involving arrays.
The first is to use a plain array of 32-bit integers, and then copy the bytes into that array:
char data[12];
// data is initialized...
uint32_t integers[3];
memcpy(integers, data, 12);
printf("First value is 0x%08x\n", integers[0]);
The other way is to use unions:
union type_punning_union
{
uint32_t integers[3];
char data[12];
};
union type_punning_union u;
// Initialize u.data...
printf("First value is 0x%08x\n", u.integers[0]);
Big important note 1: Your byte array have a size mismatch for matching all data evenly to 32-bit integers.
Big important note 2: The code shown above doesn't care about endianness, meaning the results printed might not be exactly what you expect.
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 5 years ago.
Improve this question
I am new to C. I was just interested in making a program which takes two integer input from the user and add it but the condition is that i have to use only one variable. I came up with this code:
#include <stdio.h>
int main()
{
int *a;
scanf("%d%d",a,(a+1));
printf("\nSum=%d",*a+*(a+1));
return 0;
}
scanf() function takes an valid address as an argument and i am passing the value in a(which is not initialised yet). So, how this code worked in Turbo C++?
You are trying to access an area that is not within the scope of the program. Luckily TCC gave it, but I believe if you go on experimenting, results will be undefined.
You can do something like this to solve your problem of adding using 1 variable.
int main()
{
int a;
scanf("%d",&a); //scan the first number
getchar();
a += getchar()-'0'; // get the second number (restricted to 0-9)
printf("%d",a);
return 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 8 years ago.
Improve this question
Write a C program that reads lines containing floating point values of type double one per line from the standard input ( e.g. using scanf ), converts those values to integers and then prints those values as right justified integers in a 20-character wide field one per line on the standard output.
#include <stdio.h>
My biggest problem is I don't know where to start. Any tips and help would be appreciated.
The concept is to TYPECAST the float to an integer.
The loop here is for multiple values if you want.
This is the program. I hope this helps; it runs as you want.
#include <stdio.h>
int main()
{
float n;
int t;
//loop here
scanf("%f", &n);
t = (int)n;
printf("%20d", t);
// end loop here
return 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 8 years ago.
Improve this question
I have to create a wordcount program in C using Posix. I also have to use multithreading using pthreads by reading the input file into a buffer and then paritioning that buffer given how many threads are going to be used. Then, each thread should count the number of words in its partition. The problem is i cant find any sources at all for splitting or partitioning the contents in a buffer. Any help at all would be appreciated.
Assuming your program stores the file as a pointer to an array of characters, you might achieve a similar result by representing a "partition" of the buffer as a pointer to the start of your partition and an integer to represent the size of the partition.
The code snippet below may help you with partitioning the buffer.
struct buffer_partition {
char* start;
int size;
}
void* word_count(void* arg) {
struct buffer_partition* buffer=(struct buffer_partition*)arg;
/* do word counting with buffer->start
be careful to not access characters after buffer->size */
}
int main() {
/* read input */
struct buffer_partition* partition = (struct buffer_partition*)malloc(sizeof(struct buffer_partition));
partition->start=buffer+offset;
partition->size=size_of_each_partition;
pthread_create(thread_pointer, extra_attributes, word_count, partition);
/* sum all results, print answer :) */
}