I am using turbo c++. I am getting the error "Declaration Syntax error" in FILE *fp;
the following is my code.
#include <bios.h>
#include <dos.h>
FILE *fp;
unsigned char buf[512];
unsigned char st[60];
unsigned char headno[10];
unsigned char secno[10];
unsigned char trackno[10];
void main (void)
{
int i ;
for (i=0;i<512;i++)
buf[i]=0;
gets(st);
fp=fopen(st,"wb");
printf("Head ");
gets(headno);
puts (headno);
printf("\nsector ");
gets(secno);
puts(secno);
printf("\ntrack ");
gets(trackno);
puts(trackno);
i = biosdisk(2,0x80,atoi(headno),
atoi(trackno),atoi(secno),1,buf) ;
if (*(((char *)(&i))+1)==0)
{
fwrite(buf,1,512,fp);
fclose(fp);
}
else
printf("Cannot Read Error# = %x",i);
}
The error is shown in the image1
FILE* and fopen() normally require you to #include <stdio.h>. I assume that is true for Turbo C++ too.
Related
I cannot manage to solve the error, even though I've used these types of structures in other project in the same manner and it worked.
Meanwhile I'm not getting an errors in the declaration or initialisation of my structures.
The idea is to read information about a bunch of cars from a binary file.
It tells me that "struct Car" is an incomplete type and not allowed.
Here's the code I've made so far:
car.c
#include <stdio.h>
#include <string.h>
#include "car.h"
#define MAX_NUMBER_OF_CARS 10
struct Car importedCars[MAX_NUMBER_OF_CARS];
struct Config importedConfigs[MAX_NUMBER_OF_CARS];
int main( int argc, char* argv[]){
FILE *fp;
fp = fopen("cars.bin","rb");
if(fp != NULL)
{
fread(importedCars,sizeof(struct Car),MAX_NUMBER_OF_CARS,fp);
printf("1. Binary File read, structures of cars created.\n\n");
for(int i = 0; i < MAX_NUMBER_OF_CARS; i++)
{
printf("%s\t%s\t%s\t%d\t%f\t%f\t%f\t%d\n", importedCars[i].make, importedCars[i].model, importedCars[i].configuration.fuelType, importedCars[i].power, importedCars[i].torque, importedCars[i].fuelConsumption, importedCars[i].co2PerKm, importedCars[i].price);
}
}
else
{
printf("cannot read file.\n");
}
fclose(fp);
return 0;
}
car.h
#ifndef _CAR_H_
#define _CAR_H_
#define fuelLength 10
#define gearLength 10
#define conditionLength 10
#define makeLength 30
#define modelLength 30
typedef struct {
char fuelType[fuelLength];
char gearType[gearLength];
int numGears;
char condition[conditionLength];
} Config;
typedef struct {
char make[makeLength];
char model[modelLength];
Config configuration;
short power;
float torque;
float fuelConsumption;
float co2PerKm;
int price;
} Car;
I have a header file named store.h which contains a struct and my main is located in E.c,i am currently experiencing an error which i can not comprehend why it is happening. Any help is much appreciated
store.h:
struct st{
char *buf,*var;
int line_number;
char *keywords;
char *operators;
};
E.C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "store.h"
#define max 1000000
int main(int argc,char *argv[]){
FILE *fp;
int ag=0;
fp=fopen( argv[1],"r");
if(fp==NULL){
printf("error cannot open file\n");
exit(1);
}
while(feof(fp)==0){
*buf=fgets(*buf,max,fp);
// irrelevant commands follow
}
fclose(fp);
}
On the line *buf=fgets(*buf,max,fp); I get the error:‘buf’ undeclared (first use in this function), however I have declared it in my struct and I have included the file that contains the struct, where am I wrong?
buf is a member of the struct.
You need to define the struct object.
You need to allocate memory for buf.
You should not assign buf with return value as it may cause a memory leak
Why is “while ( !feof (file) )” always wrong?
int main(int argc,char *argv[]){
FILE *fp;
struct st s;
int ag=0;
s.buf = malloc(max);
if(!s.buf) exit(2);
fp=fopen( argv[1],"r");
if(fp==NULL){
printf("error cannot open file\n");
exit(1);
}
while(fgets(s.buf,max,fp)){
/* ...*/
}
/* .... */
I have been working on reading data from a file, which contains student name and age in the format:
John
12
Jane
13
Julia
18
Here's the program I wrote:
#include <stdio.h>
#include <stdlib.h>
struct record{
char name[50];
int age;
};
int main(){
FILE *fp;
fp=fopen("sample2.txt","r");
struct record a[50];
int counter=1;
int i=0;
while (!EOF){
if (counter%2!=0){
fgets(a[i].name,50,fp);
counter++;
}
if (counter%2==0){
a[i].age=getw(fp);
counter++;
i++;
}
}
return 0;
}
However, on printing a[0].name, I am not getting expected output. Can someone help?
As already pointed out in the comments, EOF is a value defined in stdio.h and does not say anything about your file descriptor. I also would recommend you to use fscanf (As long you are sure that the names in the file are all of the correct length). fscanf takes a string similar to printf, specifying the elements you are expecting and returns the number of possible matches. Also it helps you with converting your data to the correct datatypes. So the code could look like this:
#include <stdio.h>
#include <stdlib.h>
struct record
{
char name[50];
int age;
};
int main ()
{
FILE *fp;
fp = fopen ("sample2.txt", "r");
struct record a[50];
int i = 0;
while (fscanf(fp, "%s\n%d\n", (char *) a[i].name, &a[i].age) > 0) {
i++;
}
printf("%s %d\n", a[1].name, a[1].age);
return 0;
}
I'm new to the subject of splitting files and header files in C.
I get a Segmentation fault (core dumped) when i'm trying to run main. I don't get any more errors.
I tried to trace the problam and I think it is the line:
syntax_check(fp, symb_table, &IC, &DC); in the main.c.
I'm just trying to pass from main.c the parameters *fp and another array strcture(symbol[]) to a function in syntax_check(in syntax_check.c) and do some actions.
main.c:
#include "main.h"
int main()
{
FILE *fp;
int DC=0;
int IC=100;
symbol symb_table[20];
if (!(fp=fopen("file.txt", "r")))
{
printf("Error opening file");
exit(0);
}
syntax_check(fp, symb_table, &IC, &DC);
fclose(fp);
return 0;
}
main.h:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char name[10];
int address;
int external;
int action;
} symbol;
int syntax_check(FILE*, symbol[], int*, int*);
syntax_check.c:
#include "syntax_check.h"
int syntax_check(FILE *fp, symbol symb_table[], int *IC, int *DC)
{
char buff[80]; /*line to read*/
char buff2[20]; /**/
int i=0;
fgets (buff, 80, fp);
while (buff[i]!='\0'||buff[i]!=' '||buff[i]!='\t')
{
buff2[i]=buff[i];
i++;
}
buff[i]='\0';
if (exist(buff2))
printf("legal");
else
printf("illegal");
return 0;
}
syntax_check.h:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char name[10];
int address;
int external;
int action;
} symbol;
FILE *fp;
int exist(char action[]);
I have a segmentation fault...i'm not sure what's causing it. Also, when passing the member pname into the function get_names, am I doing this correctly, or is there a better way of doing this?
#include <stdio.h>
#define MAX_NAME 20
#define MAX_PLAYRS 16
typedef struct {
char pname[MAX_NAME];
int runs;
char how_out;
} Team_t;
Team_t player[MAX_PLAYRS];
Team_t *player_ptr[MAX_PLAYRS];
void get_names (int count, char *str);
int main (void) {
int i;
for (i = 0; i < MAX_PLAYRS; i++) {
get_names(i, &(*player[i].pname));
printf("Player: %s\n", player[i].pname);
}
}
void get_names (int count, char *str) {
FILE *inp;
char status;
inp = fopen("teamnames.rtf", "r");
status = fscanf(inp, "%s", str);
if (status == EOF) {
count = MAX_PLAYRS;
}
}
With your code unchanged, I get a segmentation fault if the file can't be opened properly (i.e. it's unreadable due to permissions, or it simply does not exist).
Here's a modified version of you function get_names():
void get_names(int count, char *str)
{
FILE *inp;
inp = fopen("teamnames.rtf", "r");
if (inp == NULL) {
perror("Failed");
return;
}
fgets(str, MAX_NAME, inp);
fclose(inp);
}
This would still read the first name 16 times, but it would tell you why it didn't manage to open the file. To read the next name from the file (rather than repeatedly the first name), open (and close) the file in the main() function instead.
Also, you might as well call get_names() like so:
get_names(i, player[i].pname);
No need to do that &(*...) thing you're doing.
And, finally, I hope that the teamnames.rtf file is not actually an RTF file, but a simple text file with a name on each line.
The problem comes from this line:
get_names(i, &(*player[i].pname));
Understanding pointers and dereferencing is one of the biggest adjustments to learning C if you are switching from another language. You're doing it wrong, and I think you should seek out a tutorial on the subject. Try http://www.cplusplus.com/doc/tutorial/pointers/ as a starting point.
Get a debugger to tell you what is wrong. Compile the code with debugging enabled (see you man page for your compiler) and run something like this:
gdb a.out core
Then you should be able to see which line made the code core dump. You could use idb as well, if you have the intle compiler installed. This is, of course, on *nix. If you are talking windows, use the VS debugger.
In addition do NOT use fscanf as it is unsafe. Use fgets instead.
There are many strange things. First thing is, it seems like the names are in a file, but what you are doing is in every iteration of your for loop, you call get_names which opens the file again, that is goes to the beginning of the file and you read the same name over and over again.
That is if you closed the file. Since you haven't closed the file, the file is already open and you keep reopening it (which could be the cause of your problem)
Another thing is, how can
if (status == EOF) {
count = MAX_PLAYRS;
}
Give you the current count? Regardless of the count of the players in the file, you are just setting it to MAX_PLAYERS.
Another thing is that count is an input to the function that is not a pointer, so setting it does not change the value outside the function (which is what I assumed you wanted).
Here is how I would do it with minimum change to your code:
#include <stdio.h>
#define MAX_NAME 20
#define MAX_PLAYRS 16
typedef struct {
char pname[MAX_NAME];
int runs;
char how_out;
} Team_t;
Team_t player[MAX_PLAYRS];
Team_t *player_ptr[MAX_PLAYRS];
void get_names (int count, char *str, FILE *inp);
int main (void) {
FILE *inp;
int i;
int count;
inp = fopen("teamnames.rtf", "r");
for (i = 0; i < MAX_PLAYRS; i++) {
get_names(&count, player[i].pname, inp);
printf("Player: %s\n", player[i].pname);
}
}
void get_names (int *count, char *str) {
char status;
status = fscanf(inp, "%s", str);
if (status == EOF) {
*count = MAX_PLAYRS;
}
}
Here is how I would do it more concisely:
#include <stdio.h>
#define MAX_NAME 20
#define MAX_PLAYRS 16
typedef struct {
char pname[MAX_NAME];
int runs;
char how_out;
} Team_t;
Team_t player[MAX_PLAYRS];
Team_t *player_ptr[MAX_PLAYRS];
int get_names (Team_t *team);
int main (void) {
get_names(player);
}
int get_names (Team_t *team) {
int i = 0;
FILE *inp;
inp = fopen("teamnames.rtf", "r");
while (i < MAX_PLAYRS && !feof(inp) {
fscanf(inp, "%s", team[i].pname);
printf("Player: %s\n", player[i].pname);
}
}
Note that the problems with fscanf, checking array boundaries etc are not the concern of this solution, but this rather gives you the idea of what to do not a code for you to copy-paste.