Read from a file until a space is found [closed] - c

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
I'm new to C programming, so I wonder if it's possible to read from a certain file using the system call read() until a space is found.
Example:
A file contains a number and a process PID (59 5542). I want to read first the number, saving it into a variable and then do the same thing with the PID.
Thanks in advance.
P.S: since this is an exercise for my Operating Systems class, I have to use read().

I recommend first reading the number and the PID with one read() call into a sufficiently large buffer, then saving both into variables, e. g.:
char buf[20] = { 0 }, *end;
read(fd, buf, sizeof buf - 1);
int num, PID;
num = strtol(buf, &end, 0);
PID = strtol(end, NULL, 0);

Related

Issues dynamically allocating strings with C [closed]

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 10 days ago.
The community is reviewing whether to reopen this question as of 10 days ago.
Improve this question
I know strings end with '/0' in C. While I'm dynamically allocating memory for a string, how do I handle that?
Also, how would you print out a dynamically allocated string? Because I've tried the regular way to print out a string and it did not work.
For the first question, I tried:
int len;
scanf("%d", &len);
char* str = (char*)malloc(sizeof(char)*len);
for (int i = 0; i<len; ++i){
scanf("%c", (str+i));
}
For the second question, I tried
printf("%s", *str);

Undestanding for loop in pipe creation in C [closed]

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 3 years ago.
Improve this question
I have a comprension question about this code that I am currently studying: this function is used in a C Shell implementation to execute piped commands. I can't understand how the person who wrote it got to know how many pipes to close (why is the limit 2*com- 2)?
for(i = 0; i < 2*com - 2; i++) close(pip[i]);
for(i = 0; i < com; ++i) {
waitpid(pid, &status, WUNTRACED);
In this program, num_pipe is not actually the number of pipes
but the number of commands (very bad name indeed!).
Between two commands you need one pipe, between three commands
you need two pipes ... between N commands you need N-1 pipes.
Each pipe relies on two file descriptors (one for reading, one
for writing) thus 2*(num_pipe-1) file descriptors are needed
for num_pipe commands.
note: the malloc() does not allocate an array of integer pointers
(as stated in the question) but an array of integers.
Following this logic, I would have written
for(i = 0; i < 2*(num_pipe-1); i += 2)
but 2*(num_pipe-1) equals to 2*num_pipe-2 and since the step
is 2, the loop condition is the same with the limit
2*num_pipe-3.
It's just terribly confusing in my opinion.

Removing bytes from even adresses in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have to remove from a file the bytes found at even addresses in C. I have opened the file for rw, I have found its length and put the content in a buffer. How can I loop every byte? I have tried this, to see what do I have in buffer:
for(i=0;i<len;i=i+2)
printf("%d",buffer[i])
But in the buffer are saved the ASCII codes of the characters. Does it have any impact on my future line codes? And I have to write the desired output in another file, or I can just erase the initial content of the file and write in the file the modified buffer?
If you intend to write the buffer back to the file, then simply overwrite the buffer in this manner:
int front = 0;
int back = 0;
while (front < len) {
buffer[back] = buffer[front];
back++;
front += 2;
}
int newLen = back;
// Write the buffer to disk using the the new length (newLen)

C: read binary file into struct [closed]

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);

How to partition the contents in a buffer in C [closed]

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 :) */
}

Resources