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 :) */
}
Related
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);
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)
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);
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 7 years ago.
Improve this question
I wanna get some numbers from keyboard. But how to store that number without array[] ? Have i chance to do that ? I don't know exact how many numbers come from keyboard. If i had permission of array, its simple. But array is not allowed.
In your situation, I'd still go with arrays, but if you insist on using pointers, this code below will help you. Regardless of whether you need arrays or pointers, you still need to define an upper limit on how many elements can be stored in memory.
It is now up to you to modify the code to make it efficient and pretty to your assignment needs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int numelements=10;
int curelement=0;
int* data=calloc(1,numelements*sizeof(int));
int* p=data;
int* res=data;
while (curelement < numelements){
scanf("%d",p);
if (*p==0){break;} //exit if number entered is zero.
p++;
curelement++;
}
//print results
while(*res != 0){
printf("%d ",*res);
res++;
}
free(data);
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;
}