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)
Related
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 26 days ago.
Improve this question
I am using char arr[] insidea struct
I used temporary array to get string from user and it works fine ,
when I try to copy this temporary array to the variable array inside the struct I get \command files at end of string
for (u32 i=0;i<name_size;i++)
{
printf("\nin While");
pn->name[i]=temp_name[i];
}
if you must do it byte by byte then do this
u32 i;
for (i=0;i<name_size;i++)
{
printf("\nin While");
pn->name[i]=temp_name[i];
}
pn->name[i] = '\0';
ie - add the trailing zero. simpler would be strcpy
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 1 year ago.
Improve this question
wanna access multiple files using C.
For suppose, I have file with that names
1.txt
2.txt
n.txt
I am looping through all of the files till n. But I am only getting data from the first file which is 1.txt. and that data is repeating n times. (n represents the number of files).
So, how to get data from each file. Each file contains different data.
for(i = 0; i < fileQuantity; i++) {
sprintf(buffer, "%d", i);
ptr = fopen(strcat("C:\\TURBOC3\\FILES\\", strcat(buffer, ".txt")), "r");
fscanf(ptr, "%s", &adminUsername);
fclose(ptr);
outtextxy(225, 140 + distance, adminUsername);
distance += 30;
}
I am surprised that you can read even from one file :). strcat("C:\\TURBOC3\\FILES\\", will not work as it invokes Undefined Behaviour (attempt o modify string literal, access out of bounds).
Simply do:
sprintf(buffer, "C:\\TURBOC3\\FILES\\%d.txt", i);
ptr = fopen(buffer, "r");
or better
snprintf(buffer, buffer_length, "C:\\TURBOC3\\FILES\\%d.txt", i);
I would suggest using something more modern than TurboC 3 :) (at least from this century)
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
This function make a strange error after using it several times and I really can't understand the reason behind it.
char *get_range(char *str,int min,int max){
char *_res=(char *)malloc(sizeof(str));
int cur=0;
while (min<max){
_res[cur]=str[min];
min++;
cur++;
}
return _res;
}
The problem is that after using this function several times, the output comes with additional chars and I don't understand why.
Notice: The additional chars are allway used returned by the function beffor
char *_res=(char *)malloc(sizeof(str));
is wrong. sizeof(str) is measuring the size of a char pointer. This is either 4 or 8 (typically) depending on your system (32 or 64 bit).
You need
char *_res=(char *)malloc(strlen(str) + 1);
strlen returns the number of characters in the string, and you need to add 1 for the terminating 0;
Second you have to add a terminating zero at the end, do:
_res[cur] = '\0';
before returning
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 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 :) */
}