I'm trying to make a Bubble Sort program in a binary file without using any arrays, instead of I will use fseek and fwrite functions.
Here is my code:
typedef struct{ //Films
short int year;
char title[LEN_NAME];
Genero_t gendre;
float rateIMDB;
}Ficha_t;
FILE * fd;
fd = fopen(name,"r+b");
Ficha_t aux1;
Ficha_t aux2;
int i,j,len;
if (fd != NULL)
{
rewind(fd);
fseek(fd, 0, SEEK_END);
len=ftell(fd);
rewind(fd);
for(i=0;i<len;i++);
{
for(j=0;j<len-1;j++)
{
fread(&aux1,sizeof(Ficha_t),1,fd);
fread(&aux2,sizeof(Ficha_t),1,fd);
if(strcmp(aux1.title,aux2.title)<0)
{
fseek(fd,-sizeof(Ficha_t)*2,SEEK_SET); //returning 2 positions for overwriting
fwrite(&aux2, sizeof(Ficha_t), 1, fd);
fwrite(&aux1, sizeof(Ficha_t), 1, fd);
fseek(fd,-sizeof(Ficha_t),SEEK_SET); //returning 1 position for checking again with the next film
}
}
}
}
It's not working since it's displaying the same 2 films, Where am I wrong? What could I do?
As mentioned in the comments, you want:
len=ftell(fd) / sizeof(Ficha_t);
to get the number of records in the file.
If you are trying to seek with offsets to the current location, use SEEK_CUR, not SEEK_SET.
I would add a bunch of printfs for ftell(fd) / sizeof(Ficha_t) so you can see that it's doing everything correctly. Also, you could be checking the return values of functions you are calling (they were probably returning errors as you tried to seek before the beginning and read/write there)
Specific code example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
void fbubble_sort(const char *filename, size_t rec_size, bool (*needSwap)(const void *, const void *)){
FILE *fp = fopen(filename, "r+b");
if(!fp){
perror("fopen, r+");
exit(EXIT_FAILURE);
}
void *first = malloc(rec_size);
void *second = malloc(rec_size);
if(!first || !second){
perror("malloc");
exit(EXIT_FAILURE);
}
enum { NO_SWAP = -1};
long last_swap_pos;
do {
rewind(fp);
long pos_1 = 0;//first record position
last_swap_pos = NO_SWAP;
if(0==fread(first, rec_size, 1, fp))
break;
while((pos_1 <= last_swap_pos || last_swap_pos == NO_SWAP) && 1==fread(second, rec_size, 1, fp) ){
if(needSwap(first, second)){
fseek(fp, pos_1, SEEK_SET);
fwrite(second, rec_size, 1, fp);
fwrite(first, rec_size, 1, fp);
fflush(fp);
last_swap_pos = pos_1;
} else {
//exchange buffer
void *temp = first;
first = second;
second = temp;
}
pos_1 += rec_size;
}
}while(last_swap_pos != NO_SWAP);
free(first);free(second);
fclose(fp);
}
#define DATA_FILE "test.dat"
#define GREATERi(type, member)\
bool greater_i(const void *first, const void *second){\
return ((type*)first)->member > ((type*)second)->member;\
}\
/**/
#define GREATERs(type, member)\
bool greater_s(const void *first, const void *second){\
return strcmp((type*)first)->member, ((type*)second)->member) > 0;\
}\
/**/
#define LESSi(type, member)\
bool less(const void *first, const void *second){\
return ((type*)first)->member < ((type*)second)->member;\
}\
/**/
typedef struct {
unsigned v;
} Record;
GREATERi(Record, v)
int main(void){
void make_test_data(void);
void print_test_data(void);
make_test_data();
print_test_data();
fbubble_sort(DATA_FILE, sizeof(Record), greater_i);
print_test_data();
}
void make_test_data(void){
FILE *fp = fopen(DATA_FILE, "wb");
if(!fp){
perror("fopen, w");
exit(EXIT_FAILURE);
}
Record rec;
for(int i = 0; i < 100; ++i){
rec.v = rand() % 100;
fwrite(&rec, sizeof(rec), 1, fp);
}
fclose(fp);
}
void print_test_data(void){
FILE *fp = fopen(DATA_FILE, "rb");
if(!fp){
perror("fopen, r");
exit(EXIT_FAILURE);
}
Record rec;
int cnt = 0;
while(fread(&rec, sizeof(rec), 1, fp)){
if(cnt)
putchar(' ');
printf("%2u", rec.v);
if(++cnt == 10){
putchar('\n');
cnt = 0;
}
}
putchar('\n');
fclose(fp);
}
Related
This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
How Can I debug a C program on Linux?
(4 answers)
Closed 1 year ago.
I have to build a program which takes in the vertices and edges from a csv file and uses an adjacency matrix to store the distance from one vertex to another and calls the function shortest_path which uses the dijkstra's algorithm to find the shortest path and calls the printpath function to print the information of all the vertices it goes through to get from the origin to the end.The information about the vertices is stored in the array of structures arr[].
The problem is that the program stops working when main() call the shortest_path() and the return value is 3221225725
The shortest_path function runs on its own in another program I made but is not called in the main when I execute this program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BUFFER 100
#define MAX_NB 8000
#define INFINITY 9999
typedef struct edges edges;
struct edges{
int from,to,weight;
};
typedef struct stops stops;
struct stops {
int id;
float lat,lont;
char title[MAX_BUFFER];
};
stops* arr[MAX_NB]={0};
typedef struct Graph{
int vertices;
// int visited;
} Graph;
int visited[MAX_NB];
int amatrix[MAX_NB][MAX_NB];
int n;
Graph* create_graph(int num_nodes){
Graph* g = (Graph *)malloc(sizeof(struct Graph));
g->vertices=num_nodes;
int i,j;
for(i=0;i<num_nodes;i++){
for(j=0;j<num_nodes;j++){
amatrix[i][j]=0;
}
}
n=num_nodes;
return g;
}
Graph *graph;
int next_field( FILE *f, char *buf, int max ) {
int i=0, end=0, quoted=0;
for(;;) {
// fetch the next character from file
buf[i] = fgetc(f);
// if we encounter quotes then flip our state and immediately fetch next char
if(buf[i]=='"') { quoted=!quoted; buf[i] = fgetc(f); }
// end of field on comma if we're not inside quotes
if(buf[i]==',' && !quoted) { break; }
// end record on newline or end of file
if(feof(f) || buf[i]=='\n') { end=1; break; }
// truncate fields that would overflow the buffer
if( i<max-1 ) { ++i; }
}
buf[i] = 0; // null terminate the string
return end; // flag stating whether or not this is end of the line
}
void fetch_stops ( FILE *csv, struct stops *p) {
char buf[MAX_BUFFER];
next_field( csv, buf, MAX_BUFFER );
p->id = atoi(buf);
next_field( csv, p->title, MAX_BUFFER );
next_field( csv, buf, MAX_BUFFER );
p->lat = atof(buf);
next_field( csv, buf, MAX_BUFFER );
p->lont = atof(buf);
}
void fetch_edges ( FILE *csv, struct edges *p) {
char buf[MAX_BUFFER];
next_field( csv, buf, MAX_BUFFER );
p->from = atoi(buf);
next_field( csv, buf, MAX_BUFFER );
p->to = atoi(buf);
next_field( csv, buf, MAX_BUFFER );
p->weight = atoi(buf);
}
void print_stops( struct stops *p ) {
printf("%d \t \t %s \t %f %f\n",
p->id,p->title, p->lat, p->lont);
}/*
void print_edges( struct edges *p ) {
printf("%d \t \t %d \t %d\n",
p->from,p->to, p->weight);
}
*/
int load_vertices(char *fname){
FILE *f;
struct stops pArray[MAX_NB];
struct stops p;
f=fopen(fname,"r");
if(!f) {
printf("unable to open file\n");
return 0;
}
fetch_stops( f, &p ); // discard the header data in the first line
int ngames = 0;
while(!feof(f)) {
fetch_stops( f, &pArray[ngames]);
arr[ngames]=&pArray[ngames];
ngames++;
}
printf("loaded %d vertices\n",ngames);
fclose(f);
graph = create_graph(ngames);
return 1;
}
void add_edge(int from, int to, int weight){
amatrix[from][to]=weight;
amatrix[to][from]=weight;
}
int load_edges(char *fname/*,Graph *g*/){
FILE *f;
struct edges pArray[MAX_NB];
struct edges p;
f=fopen(fname,"r");
if(!f) {
printf("unable to open file\n");
return 0;
}
fetch_edges( f, &p ); // discard the header data in the first line
int nedges = 0;
int from,to,weight;
while(!feof(f)) {
fetch_edges( f, &pArray[nedges]);
nedges++;
}
int i;
for(i=0;i<nedges;i++){
add_edge(pArray[i].from,pArray[i].to,pArray[i].weight);
}
printf("loaded %d edges\n",nedges);
fclose(f);
return 1;
}
void printpath(int parent[], int u){
// Base Case : If j is source
if (parent[u] == - 1)
return;
printpath(parent, parent[u]);
printf("%d %s\n", arr[u]->id, arr[u]->title);
}
void shortest_path(int origin, int end){
printf("Works1");
int distance[MAX_NB];
int pred[MAX_NB];
int cost[MAX_NB][MAX_NB];
int count,minD,nextn,i,j;
pred[0]=-1;
int n=MAX_NB;
printf("Works2");
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
if (amatrix[i][j] == 0)
cost[i][j] = INFINITY;
else
cost[i][j] = amatrix[i][j];
}
}
for (i = 0; i <n; i++) {
distance[i] = cost[origin][i];
}
printf("Works1");
distance[origin] = 0;
printf("Works2");
visited[origin] = 1;
count = 1;
while (count < n - 1) {
minD = INFINITY;
for (i = 0; i < n; i++){
if ((distance[i] < minD) && (visited[i])!=1) {
minD = distance[i];
nextn = i;
}}
visited[nextn] = 1;
for (i = 0; i < n; i++)
if (!(visited[i]))
if (minD + cost[nextn][i] < distance[i]) {
distance[i] = minD + cost[nextn][i];
pred[i]=nextn;
}
count++;
}
printf("Works");
printpath(pred,end);
}
int main () {
load_vertices("vertices.csv");
load_edges("edges.csv")
printf("%d",amatrix[300][7490]);
shortest_path(300,253);
return EXIT_SUCCESS;
}
iv tried a lot of solutions to try to get this working (i.e using memcpy etc) I cant seem to find the issue, depending on what I try I either end up with gibberish or SEGV
iv spent a lot of time already googling and trying different ways, i still cant figure out why the arrays won't combine successfully
#include <stdio.h>
#include <stdint.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <unistd.h>
#define log_info printf
typedef struct
{
char* name;
//size_t size;
} entry_t;
/* qsort struct comparison function (C-string field) */
static int struct_cmp_by_name(const void* a, const void* b)
{
entry_t* ia = (entry_t*)a;
entry_t* ib = (entry_t*)b;
return strcmp(ia->name, ib->name);
/* strcmp functions works exactly as expected from comparison function */
}
entry_t* get_item_entries(const char* dirpath, int* count)
{
struct dirent* dent;
char buffer[512]; // fixed buffer
int dfd = 0,
n, r = 1; // item counter, rounds to loop
entry_t* p = NULL; // we fill this struct with items
loop:
n = 0;
printf("loop: %d, count:%d\n", r, *count);
// try to open dir
dfd = open(dirpath, O_RDONLY, 0);
if (dfd < 0)
{
printf("Invalid directory. (%s)\n", dirpath);
*count = -1;
return NULL;
}
else
{
printf("open(%s)\n", dirpath);
}
memset(buffer, 0, sizeof(buffer));
while (syscall(SYS_getdents, dfd, buffer, sizeof(buffer)) != 0)
{
dent = (struct dirent*)buffer;
while (dent->d_fileno)
{ // skip `.` and `..`
if (!strncmp(dent->d_name, "..", 2)
|| !strncmp(dent->d_name, ".", 1)) goto skip_dent;
// deal with filtering outside of this function, we just skip .., .
switch (r)
{ // first round: just count items
case 1:
{
// skip special cases
if (dent->d_fileno == 0) goto skip_dent;
break;
}
// second round: store filenames
case 0: p[n].name = strdup(dent->d_name); break;
}
n++;
skip_dent:
dent = (struct dirent*)((void*)dent + dent->d_reclen);
if (dent == (void*)&buffer[512]) break; // refill buffer
}
memset(buffer, 0, sizeof(buffer));
}
close(dfd);
// on first round, calloc for our list
if (!p)
{ // now n holds total item count, note it
p = calloc(n, sizeof(entry_t));
*count = n;
}
// first round passed, loop
r--; if (!r) goto loop;
// report count
printf("%d items at %p, from 1-%d\n", *count, (void*)p, *count);
/* resort using custom comparision function */
qsort(p, *count, sizeof(entry_t), struct_cmp_by_name);
// report items
//for (int i = 0; i < num; ++i) log_error( "%s", p[i].name);
return p;
}
int main(int argc, char* argv[])
{
int HDD_count = -1;
uint32_t total = -1;
int ext_count = -1;
entry_t* e = NULL;
entry_t *HDD = get_item_entries("/mnt/f/n", &HDD_count);
entry_t* ext = get_item_entries("/mnt/f/dls", &ext_count);
total = ext_count + HDD_count;
e = (entry_t*)malloc(sizeof *e * total);
if (e != NULL)
{
for (int i = 1; i < HDD_count; i++)
{
log_info("HDD[%i].name %s\n", i, HDD[i].name);
e[i].name = strdup(HDD[i].name);
}
for (int i = 1; i < ext_count; i++)
{
log_info("ext[%i].name %s\n", i, ext[i].name);
e[i + HDD_count].name = strdup(ext[i].name);
}
}
else
printf("Failed to Allocate the Array");
char tmp[256];
int i = 1, j;
for(j = 1; j <= total; j++)
{
snprintf(&tmp[0], 255, "%s", e[ j].name);
log_info("%i:%s\n", j , tmp);
}
return 0;
}
Here is a rewrite of a snippet of main() that I mentioned in my comment above:
#define CHECK(p, msg) if(!(p)) { printf("%s:%d: %s", __FILE__, __LINE__, msg); return 1;}
...
entry_t *HDD = get_item_entries("/mnt/f/n", &HDD_count);
CHECK(HDD, "HDD failed to get entries");
entry_t *ext = get_item_entries("/mnt/f/dls", &ext_count);
CHECK(ext, "ext failed to get entries");
uint32_t total = HDD_count + ext_count;
entry_t *e = malloc(total * sizeof(*e));
CHECK(e, "malloc failed");
for(int i = 0; i < HDD_count; i++) {
log_info("HDD[%i].name %s\n", i, HDD[i].name);
e[i].name = strdup(HDD[i].name);
}
// write a function instead of duplicating code?
for (int i = 0; i < ext_count; i++) {
log_info("ext[%i].name %s\n", i, ext[i].name);
e[HDD_count + i].name = strdup(ext[i].name);
}
It looks like a short lived program, but I would still free the values from strdup() and e itself.
Is this possible, what's wrong here!?
I need this to read the list of "items" that belongs inside one node of "Orcamento", and also the list of "decisores".
int lerDadosO(DLLIST3 *orcamentos) {
int nOrcamentos = 0, i = 0;
ORCAMENTO Orcamento;
FILE *ficheiro = fopen("dadosO.bin", "rb");
if(ficheiro != NULL) {
fseek(ficheiro, 0L, SEEK_END);
nOrcamentos = ftell(ficheiro) / sizeof(ORCAMENTO);
rewind(ficheiro);
while (i != nOrcamentos) {
Orcamento.itens = createI();
Orcamento.decisores = createU();
fread(&Orcamento, sizeof(ORCAMENTO), 1, ficheiro);
// viewI(Orcamento.itens, listarItem);
if(insertendO(orcamentos, Orcamento) != 0) {
printf("Ocorreu um Erro!");
return 0;
}
i++;
}
fclose(ficheiro);
}
else {
printf("Não existe nenhum ficheiro binário!");
}
return nOrcamentos;
}
I'm not sure how you have implemented your list system, this may be accomplished by first reading data to a buffer, than transforming that into a "list"
struct record {
int field0
int field1;
};
int main(void) {
// variables
FILE *stream; // reading data
struct record data[length]; // writing data
struct mylisttype *list; // result
...
// read `length` `struct record`s from `stream`
fread(data, sizeof(struct record), length, stream);
listfrombuffer(buffer, list);
...
}
I am very new to coding and I'm trying to learn how a compiler thinks.
Anyway, I wrote this code for school where the code is supposed to gather random data from an input.txt file, sort by insertion and then write the sorted version to an output.txt file. Also, the data needs to be 64-bits. I have two problems with the code. One is that during the function that writes it says '.exe has triggered a break point'. I don't understand why this is.
The second is that it says I haven't defined the variable key, even though I think I did. Below is my code. Any help would be appreciated as I'm very new to this.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
typedef uint64_t u64;
struct file_contents
{
u64 *Data;
u64 Size;
};
file_contents
ReadFile(const char* FileName)
{
file_contents Contents = {};
FILE* File = fopen(FileName, "rb");
if (File)
{
fseek(File, 0, SEEK_END);
Contents.Size = ftell(File);
fseek(File, 0, SEEK_SET);
Contents.Data = (u64*)malloc(Contents.Size);
if (Contents.Data)
{
fread(Contents.Data, 1, Contents.Size, File);
}
fclose(File);
}
return Contents;
}
u64*
SortFile(file_contents Contents)
{
u64 key;
for (int i = 1; i < 100; ++i)
{
Contents.Data[i] = key;
int j;
j = i - 1;
while (i >= 0 && Contents.Data[j] > key)
{
j = j - 1;
key = Contents.Data[j + 1];
}
}
return Contents.Data;
}
void
WriteFile(const char* FileName, file_contents Contents)
{
FILE* File = fopen(FileName, "w");
if (File)
{
if (Contents.Data)
{
fwrite(Contents.Data, 1, Contents.Size, File);
}
fclose(File);
}
}
void
CloseFile(file_contents Contents)
{
if (Contents.Data)
{
free(Contents.Data);
Contents.Data = 0;
Contents.Size = 0;
}
}
void
WriteSortedFile(const char* InputFileName, const char* OutputFileName)
{
file_contents File = ReadFile(InputFileName);
SortFile(File);
WriteFile(OutputFileName, File);
CloseFile(File);
}
int main()
{
file_contents Contents = ReadFile("input.txt");
WriteSortedFile("input.txt", "output.txt");
}
I code C to get better in programming and study... and have program that should generate a static web-page. It also saves the project as a text-file. I have separate functions to make object (realloc and put a new struct...), and have extracted the problem-code to a short program for this occasion... It's just for reading the 'project'. When I run it says:
Segmentation fault (core dumped)
in the middle of the print_1_content
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SELL_ITEM 1
#define PARAGRAPH_ITEM 2
struct SellItem {
char title[50];
int nr_of_img;
char ** image_files;//array of strings
};
struct ParagraphItem{
char * text;
};
union ContentItem{//one of the following only
struct SellItem s_item;
struct ParagraphItem p_item;
};
struct Content{
int type;//1=sellitem 2=paragraph
union ContentItem c_item;
};
int open_items_file(struct Content **, int *, char *);
int free_1_item(struct Content *);
struct Content import_1_content(char *);
void increase(struct Content**, int *);
void print_1_content(struct Content *);
struct Content import_1_content(char *);
int free_1_item(struct Content *);
int main (void)
{
struct Content * content;
int content_count=0;
open_items_file(&content, &content_count, "all_items.txt");
return 0;
}
int open_items_file(struct Content ** content, int * number_of_content, char * filename){
printf("open_items_file %s\n", filename);
FILE *fp = fopen(filename, "r");
char * line = NULL;
size_t len = 0;
ssize_t read;
int counter=0;
if(fp==NULL){
return 0;
}
//for each row
while ((read = getline(&line, &len, fp)) != -1) {
if((line[0]=='S' || line[0]=='P') && line[1]=='-'){
if(line[3]==':'){
if(line[2]=='I'){
increase(content, number_of_content);
*content[(*number_of_content)-1] = import_1_content(line);
}
else{
//not sell/paragraph item
}
}//end if line[3]==':'
}//end if line[0] =='S' eller 'P'
counter++;
}
free(line);
fclose(fp);
return counter;
}
void increase(struct Content** content, int *nr_of_content){
if((*nr_of_content)==0){
*content = malloc(sizeof(struct Content));
}
else{
*content = realloc(*content, (*nr_of_content+1) * sizeof(struct Content));
}
(*nr_of_content)++;
}
void print_1_content(struct Content * content){
//Print info
}
struct Content import_1_content(char * text_line){
struct Content temp_content_item;
char * line_pointer = text_line;
char c;
line_pointer += 4;
if(text_line[0]=='S'){
temp_content_item.type = SELL_ITEM;
temp_content_item.c_item.s_item.nr_of_img=0;
int i=0;
char * temp_text;
while(*line_pointer != '|' && *line_pointer != '\n' && i < sizeof(temp_content_item.c_item.s_item.title)-1){
temp_content_item.c_item.s_item.title[i] = *line_pointer;
i++;//target index
line_pointer++;
}
temp_content_item.c_item.s_item.title[i]='\0';
i=0;
//maybe images?
short read_img_counter=0;
if(*line_pointer == '|'){
line_pointer++; //jump over '|'
//img-file-name separ. by ';', row ends by '\n'
while(*line_pointer != '\n'){//outer image filename -loop
i=0;
while(*line_pointer != ';' && *line_pointer != '\n'){//steps thr lett
c = *line_pointer;
if(i==0){//first letter
temp_text = malloc(2);
}
else if(i>0){
temp_text = realloc(temp_text, i+2);//extra for '\0'
}
temp_text[i] = c;
line_pointer++;
i++;
}
if(*line_pointer==';'){//another image
line_pointer++;//jump over ';'
}
else{
}
temp_text[i]='\0';
//allocate
if(read_img_counter==0){//create array
temp_content_item.c_item.s_item.image_files = malloc(sizeof(char*));
}
else{//extend array
temp_content_item.c_item.s_item.image_files = realloc(temp_content_item.c_item.s_item.image_files, sizeof(char*) * (read_img_counter+1));
}
//allocate
temp_content_item.c_item.s_item.image_files[read_img_counter] = calloc(i+1, 1);
//copy
strncpy(temp_content_item.c_item.s_item.image_files[read_img_counter], temp_text, strlen(temp_text));
read_img_counter++;
temp_content_item.c_item.s_item.nr_of_img = read_img_counter;
}
}
else{
printf("Item had no img-files\n");
}
}
else{ // text_line[0]=='P'
temp_content_item.type = PARAGRAPH_ITEM;
temp_content_item.c_item.p_item.text = calloc(strlen(text_line)-4,1);
int i=0;
while(*line_pointer != '\0' && *line_pointer != '\n'){
temp_content_item.c_item.p_item.text[i] = *line_pointer;
i++;
line_pointer++;
}
}
print_1_content(&temp_content_item);
return temp_content_item;
}
int free_1_item(struct Content * item){
if(item->type==SELL_ITEM){
if(item->c_item.s_item.nr_of_img > 0){
//Freeing img-names
for(int i=0; i<item->c_item.s_item.nr_of_img; i++){
free(item->c_item.s_item.image_files[i]);
}
}
return 1;
}
else if(item->type==PARAGRAPH_ITEM){
//freeing p_item
free(item->c_item.p_item.text);
return 1;
}
else{
printf("error: unknown item\n");
}
return 0;
}
The text file to read (all_items.txt) is like this, ends with a new-line, for the two content types of "sell-item" and "paragraph-item":
S-I:Shirt of cotton|image1.jpg;image2.jpg;image3.jpg
P-I:A paragraph, as they are called.
S-I:Trousers, loose style|image4.jpg
So the problem as you've discovered is on this line:
*content[(*number_of_content)-1] = temp_content_item2;
It's because of operate precedence because *content[(*number_of_content)-1] is not the same as (*content)[(*number_of_content)-1], it's actually doing *(content[(*number_of_content)-1]). So your code is doing the array indexing and then de-referencing which is pointing at some random place in memory. Replace that line with this and that will fix the current problem.
(*content)[(*number_of_content)-1] = temp_content_item2;