C - accessing a structure array - c

I'm trying to code my own Turing machine. My program takes two files as arguments: my initial tape the machine will have to work with, and a rule file. This rule file consists in one rule per line, and each rule is five integers: current state, symbol found, new symbol, direction for the head to go, and new state. For that, I have a function that reads my file and puts each set of five ints found in a rule structure. An array of these structures is then generated.
What I'm trying to do is to return this array in order to be able to use it later on. Here is what I have :
struct rule {
int cur_state;
int symbol;
int new_symbol;
int direction;
int new_state;
};
typedef struct rule rule;
struct vect {
int nb_elem;
rule *p;
};
vect rule_generator (char * file_rule){
int line_number = 0;
int ligne;
char tmp;
rule rule_list[line_number];
vect output_rules;
FILE * file;
file = fopen(file_rule, "r");
for(tmp = getc(file); tmp != EOF; tmp = getc(file)){
if ( tmp == '\n')
line_number++;
}
output_rules.p = malloc(line_number*sizeof(rule));
assert(output_rules.p);
output_rules.nb_elem = line_number;
for (ligne = 0; ligne < line_number; ligne++ ){
fscanf(file, "%d %d %d %d %d", &rule_list[ligne].cur_state,
&rule_list[ligne].symbol, &rule_list[ligne].new_symbol,
&rule_list[ligne].direction, &rule_list[ligne].new_state);
}
fclose(file);
return output_rules;
}
int main (int argc, char *argv[]){
vect rule_list = rule_generator(argv[2]);
printf("symbole : %ls \n", &rule_list.p[0].symbol);
return 0;
}
As some of you may already have guessed, this doesn't print anything... I've been scratching my head for a while, trying to access my array. I could really use a hand here!

Few problems here.
You are declaring array with size 0.
int line_number = 0;
rule rule_list[line_number];
You don't need rule_list just remove it.
output_rules is no where being initilized other than memory allocating.
Solution:
I would suggest you to use output_rules for fscanf.
for (ligne = 0; ligne < line_number; ligne++ ){
fscanf(file, "%d %d %d %d %d", &output_rules.p[ligne].cur_state,
&output_rules.p[ligne].symbol, &output_rules.p[ligne].new_symbol,
&output_rules.p[ligne].direction, &output_rules.p[ligne].new_state);
}
Also did you mean to print the value of symbol?
As you are using %ls which is used to wchar_t *.
printf("symbole : %ls \n", &rule_list.p[0].symbol);
should be
printf("symbole : %d \n", rule_list.p[0].symbol);

There's lots going on here.
The main problem seems to be that this loop:
for(tmp = getc(file); tmp != EOF; tmp = getc(file)){
if ( tmp == '\n')
line_number++;
}
is going right the way through the file to the end, which means there's nothing left for fscanf() to read. If you want to read the whole file through once to find out the number of lines, and then read it again to read the content, you're going to have to either a) close and reopen it, or b) use the rewind() or fseek() function to go back to the start of the file.
(To be honest, an even better solution would be to design your code so that you don't need to read the file twice over. If you end up trying this and get stuck, ask again with a new question on this site.)
In addition, you should add these lines at the beginning of your code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
to read in the header files which define the functions you're using, and put this line after your struct vect definition to define the type:
typedef struct vect vect;
Also, please run your compiler with warnings enabled. This would have helped you find some of these problems yourself!

Related

Im trying to read a txt file into a linked list, that contains both integers and strings in a line, separated by commas

Im making a database, where the information about books and readers in a library are contained in two linked lists, and each line of the txt file contains the data of one book/peopleinlibrary.
Data of the books:
id;year;title;writer;isborrowed;\n
...
The id and the isborrowed(1or0) are integers, the rest are strings.
I know the atoi can convert the input lines to numbers, but when I use it while separating each line by commas, it just doesn't work, the printig is wrong and also i can't return with the *start/*begin pointer, where the list starts. By now im totally clueless because it's not the first method that I tried.
The part of the code:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
typedef struct Konyv{
int id;
char *year;
char *title;
char *writer;
int ki;
struct Konyv*next;
}Konyv;
int main(){
Konyv*start=NULL;
FILE*f;
const char s[1] = ";";
int i;
f=fopen("konyv.adat.txt","r+");
Konyv*u;
if(f != NULL){
char line[1000];
while(fgets(line, sizeof line, f) !=NULL){
u= (Konyv*)malloc(sizeof(Konyv));
u->id = strtok(line, s);
u->id=atoi(u->id);
printf("%d ",u->id);
u->year = strtok(NULL,s);
printf("%s ",u->year);
u->title = strtok(NULL,s);
printf("%s ",u->title);
u->writer = strtok(NULL,s);
printf("%s ",u->writer);
u->ki = strtok(NULL,s);
u->ki=atoi(u->ki);
printf("%d",u->ki);
printf("\n");
u->next=NULL;
if(start==NULL){
start=u;
}
else{
Konyv *mozgo = start;
while (mozgo->next!= NULL){
mozgo = mozgo->next;
}
mozgo->next= u;
}
}
else{
printf("Error while opening.\n");
return 0;
}
printf("\n");
//test if start pointer is right by printig the list again(HELP)
Konyv* temp;
temp=start;
while(temp!=NULL) {
printf("%d ",temp->id);
printf("%s ",temp->year);
printf("%s ",temp->title);
printf("%s ",temp->writer);
printf("%d ",temp->ki);
printf("\n");
temp = temp->next;
}
free(u);
fclose(f);
return 0;
}
#WhozCraig is correct, and borrowing from this post you can see that we simply need to copy the data at the pointer to a new chunk of memory. For this we can use the strdup function included in string.h.
For example:
u->year = strtok(NULL,s);
becomes
u->year = strdup(strtok(NULL,s));
You can find documentation on strdup here for further reference.
Also, I don't want to leave you hanging here, the code you handed over didn't compile cleanly -- I had to complete some brackets.
One final thing storing anything other than an int in your id field is problematic. So,
u->id = strtok(line, s);
is an issue. Simple fix is,
u->id = atoi(strdup(strtok(line, s)));
but that is kind of dirty and hard to read for another programmer coming in to maintain code. I would advise taking the time to declare a variable just to temporarily store the token you are eventually going to duplicate into your struct.

segmentation 11 in C

I'm writing a homework program in C. The program should take records from an input file and write those record to an output file. It seems like there is something wrong with the print_to_file function. I keep getting segmentation fault 11. Please help. My code is as below.
#include <stdio.h>
#include <stdlib.h>
typedef struct car { // create a struct type Car
char *license_plate;
int parking_spot;
int num_tickets;
int time_left;
} Car;
#define LICENSEPLATELENGTH 10
Car* import_cars(char *filename, int numCars);
void print_to_file(char* filename, Car* garage, int numCars);
int main(int argc, char * argv[]) {
if(argc != 4)
printf("Incorrect input.\n");
else {
int number = atoi(argv[1]);
Car* parked_car = (Car*)malloc(sizeof(Car) * number);
parked_car = import_cars(argv[2], number);
print_to_file(argv[3], parked_car, number);
free(parked_car);
}
return 0;
}
Car* import_cars(char* filename, int numCars)
{
Car* inCar = (Car*)malloc(sizeof(Car) * numCars);
inCar->license_plate = (char*)malloc(sizeof(char) * 8);
//Question: How do I do if I the plate length is varied. How to malloc space to it?
FILE* inFilePtr;
if((inFilePtr = fopen(filename, "r")) == NULL)
printf("Error! Unable to open file %s. Check again.\n", *filename);
else
{
int i = 0;
fscanf(inFilePtr, "%s", inCar[i].license_plate);
fscanf(inFilePtr, "%d%d%d", inCar[i].parking_spot, inCar[i].num_tickets, inCar[i].time_left);
printf("%s %d %d %d \n", inCar[i].license_plate, inCar[i].parking_spot, inCar[i].num_tickets, inCar[i].time_left);
for(i = 1; i < numCars; i++)
{
fscanf(inFilePtr, "%s", inCar[i].license_plate);
fscanf(inFilePtr, "%d%d%d", inCar[i].parking_spot, inCar[i].num_tickets, inCar[i].time_left);
printf("%s %d %d %d \n", inCar[i].license_plate, inCar[i].parking_spot, inCar[i].num_tickets, inCar[i].time_left);
}
}
fclose(inFilePtr);
return(inCar);
//free(inCar.license_plate); `
//Question: Do I need to free space here would it remove the value
//stored in the variable which passed to main?
}
void print_to_file(char* filename, Car* garage, int numCars) {
FILE* outFilePtr;
if((outFilePtr = fopen(filename, "w+")) == NULL){
printf("Error! Cannot Open File %s!", *filename);
printf("here\n");
} else {
int i = 0;
for(i = 0; i < numCars; i++) {
printf("%s\n%d %d %d\n", garage[i].license_plate, garage[i].parking_spot, garage[i].num_tickets, garage[i].time_left);
fprintf(outFilePtr, "%s\n%d %d %d\n", garage[i].license_plate, garage[i].parking_spot, garage[i].num_tickets, garage[i].time_left);
}
}
fclose(outFilePtr);
}
This is my input command.
./a.out 6 garage.txt output.txt
Here is what print in my terminal.
fi590dz 20 2 25
57fjgmc 8 0 55
7dkgjgu 25 1 15
f9e829d 1 2 60
4jgfd81 12 2 10
Segmentation fault: 11
By the way, I'm pretty new in programming and really bad with debugging. Could you give me some tips of how to debug or any debugging tools? I use a mac so gdb doesn't work.
Not a complete answer, because it’s a homework problem and you want to figure it out yourself, but here are some hints.
First, you really want to learn how to run your program in a debugger and get it to tell you which line crashed the program, and on which data.
Second, make sure you initialize the pointers for every element of the array before you try to read or write them.
Third, you’ll save yourself a lot of trouble if you initialize all your dynamic and local variables to zeroes, not garbage. It will make a lot of bugs reproducible, make a lot of bugs crash immediately instead of corrupting memory, and also make it obvious when you debug that you’re using uninitialized data.
Therefore, I suggest you get in the habit of allocating your dynamic arrays with calloc(), not malloc().
The problem lies within your parked_car = import_cars(argv[2], number); and Car* import_cars(char* filename, int numCars);functions.
Indeed in Car* import_cars(char* filename, int numCars); you are doing this:
Car inCar;
inCar.license_plate = (char*)malloc(sizeof(char) * 8);
So you are creating a local variable that is not accessible outside of the function (many different things can happen to the memory after the end of the function).
So when you do: parked_car = import_cars(argv[2], number); you are assigning to parked_car a freed variable.
A solution is to simply use the parked_caras an argument of your import_cars() function. All modifications made within the function will still be valid after it returns. So you should have:
void import_cars(char* filename, int numCars, Car* car);
For everyone who met the issue here, I found the problem in my program. The problem is that I didn't allocate space for each of the license_plate pointer in the structure. So my way to solve it is add a line as below in the for loop of the import_cars function.
inCar[i].license_plate = (char*)malloc(sizeof(char) * LICENSEPLATELENGTH);

Scanf leads to segfault

I have a text file:
In 0 John 66
In 1 May 77
In 0 Eliz 88
Out 0
Out 0
I'm trying to parse this text file using scanf, and at the moment send the values after "In" to the add function, however I'm getting a seg fault when trying to do this.
I have some code here:
A struct in a seperate header file:
typedef Officer test;
typedef struct {
test tests[6];
int s;
} copList;
And this one:
typedef struct {
char name[25];
int id;
} Officer;
Then I have my main method
int main(void) {
FILE * ptr;
char buffer [500];
char * temp;
int pos;
int grade;
char * name;
copList * L;
ptr = fopen("test.txt","r");
if(ptr == NULL)
exit(1);
temp = malloc(sizeof(char)*10);
name = malloc(sizeof(char)*10);
L = malloc(sizeof(copList));
while(fgets(buffer,500,ptr) != NULL) {
sscanf(buffer,"%s %d %s %d\n",temp,&pos,name,&grade);
add(L->tests[pos],pos,L); //this gives me a seg fault
}
free(name);
free(temp);
free(L);
fclose(ptr);
return 0;
}
In a separate c file I have the add function.(Can't be changed)
void add(Test b, int pos, copList * L) {
//code to be added here later...
}
I've tried allocating different amounts of memory, but that didn't help. Also I noticed if I set a value to pos, in the while loop, before the add function call, I don't get a seg fault, but obviously that's not what I want, because the value wouldn't change. Any help would be much appreciated.
The main problem I see with your code is that it does not check the return value of sscanf -- if sscanf returns something other than 2 or 4, that means your input is something other than what you say it is. In addition, the arrays temp and name might overflow (on inputs other than what you show), which would cause undefined behavior. Finally, the spaces and \n in the sscanf format are wrong and should be removed (though they shouldn't actually cause any problems in this case.)
So you code should be something like:
while(fgets(buffer,500,ptr) != NULL) {
int count = sscanf(buffer,"%9s%d%9s%d",temp,&pos,name,&grade);
if (count != 2 && count != 4) {
fprintf(stderr, "Invalid input line: %s", buffer);
continue; }
... do stuff with temp and pos (only use name and grade if count == 4)
in this line:
add(L->tests[pos],pos,L);
the first parameter is a copy of the 'test' struct.
It is almost always a bad idea to pass a whole struct. Much better to just pass a pointer to the struct:
add( &(L->tests[pos]), pos, L );
Then, this line has a couple of problems:
void add(Test b, int pos, copList * L) {
1) 'Test' is a non-existent type, perhaps you meant: 'test'
2) 'b' is expecting a passed struct. as mentioned above,
it is (almost) always better to pass a pointer to a struct.

Deallocation of Dynamic Arrays?

Here's the code I am working with...
while(temp[i]!=0){
while(temp[i]!=3){
FrameBuffer[a]=temp[i];
i++;
a++;
}
FrameBuffer[a]=temp[i];
printf(" Framebuffer: %s ", FrameBuffer);
result=layer1(FrameBuffer,PacketAction);
i++;
a=0;
}
The problem is that I want FrameBuffer to be reset each time it goes through the inner while loop (Size of FrameBuffer is 0 at start of loop). I've tried using free(FrameBuffer) but I get an error because I free the array more than once. I also tried writing FrameBuffer=NULL, but that did not work. Any help would be greatly appreciated. Thank you!
If you just need to initialise FrameBuffer before the start of each inner loop, memset can be used-:
while(temp[i]!=0) {
memset(FrameBuffer, 0, sizeof(FrameBuffer));
while(temp[i]!=3) {
Since you don't show the declarations or give a particularly good description of the input string, temp, it is a bit hard to know what you're up to.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
extern int somefunc(const char *fb, int pa);
extern int layer1(const char *fb, int pa);
int somefunc(const char *temp, int PacketAction)
{
int rc = 0;
char *FrameBuffer = calloc(2048, sizeof(char));
if (FrameBuffer != 0)
{
int i = 0;
while (temp[i] != '\0')
{
int a = 0;
while (temp[i] != 3)
FrameBuffer[a++] = temp[i++];
FrameBuffer[a++] = temp[i++];
FrameBuffer[a] = '\0';
assert(a < 2048);
printf("Framebuffer: %.*s\n", a, FrameBuffer);
int result = layer1(FrameBuffer, PacketAction);
if (rc == 0)
rc = result;
}
}
return rc;
}
This is probably sufficient. The function layer1 gets a null terminated string in FrameBuffer (it has no other visible way of determining how long the data is).
Note that by declaring a in the scope of the outer loop, I don't need to reinitialize it at the end of the loop. I've used a++ and i++ to increment the counters; it allows for compact notation with no loss of readability to the experienced C programmer — though I concede it can be a little more difficult for novice programmers to handle. Nevertheless, the idiom is worth learning.
I wonder if the inner loop should check for '\0' as well as 3 (aka '\3' or '\003'); I would need to know whether nulls can ever appear in temp other than at the very end. The code shown takes a lazy way out of validating for no overflow of the FrameBuffer. It would be better still if the function was told the length of the input temp:
void somefunc(const char *temp, size_t templen, int PacketAction)
The code could be written more resiliently. It could probably use strlen(temp), but it isn't absolutely clear from the available information.

Adding a struct to an array using a pointer

I am fairly new to the C language and allocating memory/using pointers in general. Anyway, I was experimenting with reading a file, putting those values in a struct, etc. I know exactly what I want to do and of course the program runs but the output is incorrect and some kind of jumbled numbers and letters.
There is a text file with new information for each line. Each line represents one object.
This is how a line in the file might look:
meat sirloin 6.55 8 8.50 4
Overall I want to be able to store all of my PRODUCT objects in an array (so I have an array of structs). So I attempted to allocate memory with a pointer, use a line count, and then send the pointer to a function called read. In read I add each struct to the array via a pointer. The program doesn't crash, the output is just not correct and I have no clue why not. It's prob something with pointers. If anyone could help me I would really appreciate it. Any help at all would be great.
//prototype
void read(pointerToArr);
typedef struct
{
char supType[15];
char prodName[15];
double wholePrice;
int quantWhole;
double retPrice;
int retProdQuantity;
}PRODUCT;
FILE *fr;
int lineCount = 0;
int main()
{
PRODUCT *ptr;
int i;
char check[50];
fr = fopen("ttt.txt", "r");
while(fgets(check, sizeof(check), fr)!= NULL)
{
if(check[0] != '\n')
{
lineCount++;
}
}
// allocate memory for array based on line count.
PRODUCT prodContainter[lineCount];
ptr = (PRODUCT*)malloc(sizeof(PRODUCT)*lineCount);
ptr = prodContainter;
read(ptr);
//print after adding to array via pointer from other
//function. this was a test.
for(i = 0; i < lineCount; i++)
{
printf("%s ", prodContainter[i].supType);
printf("%s ", prodContainter[i].prodName);
printf("%f ", prodContainter[i].wholePrice);
printf("%d ", prodContainter[i].quantWhole);
printf("%f ", prodContainter[i].retPrice);
printf("%d\n\n", prodContainter[i].retProdQuantity);
}
return 0;
}
void read(PRODUCT *pointerToArr)
{
// objective in this method is to read in data from the file, create an object for every line and
// then use the pointer array to add those objects to prodConstainer up above.
char supplyName[15];
char productName[15];
double wholeP = 0;
int quantityWhole = 0;
double retailPrice = 0;
int retailProductQuant = 0;
while(fscanf(fr, "%s %s %lf %d %lf %d", supplyName, productName, &wholeP, &quantityWhole, &retailPrice, &retailProductQuant) == 6)
{
PRODUCT record;
int i;
strcpy(record.supType, supplyName);
strcpy(record.prodName, productName);
record.wholePrice = wholeP;
record.quantWhole = quantityWhole;
record.retPrice = retailPrice;
record.retProdQuantity = retailProductQuant;
for(i = 0; i < lineCount; i++)
{
pointerToArr[i] = record;
}
}
fclose(fr);
}
You never rewind the file, so all the reading after you count the number of lines fails.
What you're printing is just what happens to be in memory.
There are many ways to fix this, of course.
Rewind the file, using rewind()
Close the file and let your read() function (whose name collides with a POSIX standard function, btw) re-open the file. This would also involve removing the scary global variable fr.
Re-structure so you never count the number of lines, by just reading and letting the ptr array grow as necessary (see realloc()).
Also, you should really avoid casting the return value of malloc() in C. This:
ptr = (PRODUCT*)malloc(sizeof(PRODUCT)*lineCount);
is better written as:
ptr = malloc(lineCount * sizeof *ptr);
This does away with the cast, and also uses sizeof on a value of the type pointed at to automatically compute the proper number of bytes to allocate.

Resources