Can a file be opened using a member of a struct ?

Can I open a file using a member of a struct like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Info
{
    int score;
    char name[] = "user.txt";
}info;

void newUser()
{
    BOOL reg = FALSE;
    char nUser[MAXNAME] = {0};
    FILE *file = fopen(info.name, "r+");
.
..
...
}


or do I have to declare a global variable char name[] = "user.txt"; ?
Last edited on
Have you tried it?

If you define name correctly, then yes. You can't use [] in a struct as the size of a struct has to be known at compile time. You can use:

 
const char *const name = "user.txt";


because the size of name is known as it is a pointer.

Last edited on
Ohh so it has to be a const or it can be as well char *const name = "user.txt"; ?
Or you can ignore the pointer syntax and just allocate:
char name[10] = "user.txt";
Btw. I tried const char *const name = "user.txt"; but it gives me an error:

main.c|39|error: expected ':', ',', ';', '}' or '__attribute__' before '=' token|
You can't use [] in a struct
in fact I did use it:

1
2
3
4
5
6
7
8
struct Info
{
    char name[50];
    char lastname[50];
    char city[50];
    char country[50];
    char tel[20];
}info;


but maybe you can't assign something to a variable in a struct such as:

1
2
3
4
5
6
7
8
struct Info
{
    char name[50] = "text.txt; // <- like that..
    char lastname[50];
    char city[50];
    char country[50];
    char tel[20];
}info; 


Or maybe I can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Info
{
    int score;
    char name";
};

void newUser()
{
    BOOL reg = FALSE;
    char nUser[MAXNAME] = {0};
    struct Info info;
    info.name = "user.txt";
    FILE *file = fopen(info.name, "r+");
.
..
... 


is this possible ?
Do you meant inside the struct jonnin?


1
2
3
4
5
struct Info
{
    int score;
    char name[10] = "user.txt";
}info;
Well in the end is it possible to do something like this:

From this:

1
2
3
4
5
6
7
8
9
10
11
12
char name[] = "user.txt";

//and use it here
newUser()
{
    BOOL reg = FALSE;
    char nUser[MAXNAME] = {0};
    FILE *file = fopen(name, "r+");
.
..
... 
}


to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Info
{
    int score;
    char name[MAXNAME]";
}info;

newUser()
{
    BOOL reg = FALSE;
    char nUser[MAXNAME] = {0};
    info.name[MAXNAME] = "user.txt";
    FILE *file = fopen(info.name, "r+");
.
..
...  


I mean to use char name[] = "user.txt" not like a global variable but instead a member of a struct ?
Last edited on
The way I did it here I get one warning:

||=== Build: Debug in New_Project1 (compiler: GNU GCC Compiler) ===|
In function 'newUser':|
warning: assignment makes integer from pointer without a cast [-Wint-conversion]|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
You can't use [] in a struct
in fact I did use it:


No you didn't. You used [50] etc where the size was specified. [] means that the size will be determined and not pre-stipulated. There is a difference.

You can certainly use [50] etc within a struct as the size is specified at compile time.

Btw. I tried const char *const name = "user.txt"; but it gives me an error:

main.c|39|error: expected ':', ',', ';', '}' or '__attribute__' before '=' token|


1
2
3
4
5
6
7
8
9
10
11
12
struct Info1
{
	int score;
	const char *const name = "user.txt";
}info1;

void newUser()
{
	BOOL reg = FALSE;
	char nUser[MAXNAME] = {0};
	FILE* file = fopen(info1.name, "r+");
}


is fine with VS (apart from warnings about unreferenced variables).
Last edited on
Ohh.. I guess I have to apologize for that,..sorry I thought [n] and [] are the same thing..and that compiler is treating both the same either if the size is determined or not.
Ohh.. but I don't understand why I get this error.. I reedit the code again and now I get another error in the void newUser(); function 3rd line FILE* file = fopen(info.name, "r+"); says:
Struct 'Info' has no member named 'name'
@@..
I did something else without getting any errors or warnings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Info
{
    int score;
    char name[MAXNAME];
}info;

void newUser()
{
    BOOL reg = FALSE;
    char nUser[MAXNAME] = {0};
    char fname[MAXNAME] = "user.txt";
    info.name[MAXNAME] = *fname;
    FILE *file = fopen(info.name, "r+");
.
..
}


I only think how to assign "user.txt" to the member "name" of the struct.

Now I only need to test it..
Last edited on
What language are you using to compile this code, C or C++? It appears to be C so assigning values in the structure definition is not allowed.

If you're trying to write C++ code you should start using C++ features, like std::string, std::streams, etc, and stop using C features like FILE, C-strings, etc.

No matter what language you are attempting to use you should stop using global variables, ie: info is a global variable in your code.

Okay.. atm. I'm a beginner and I start to learning C, maybe later I'll pass to C++, but for now I'll stik with it and if you say so, then it's useless what I'm trying to do, I'll never be able to do that with a structure. I'm going to use a global variable JUST ONE
const char name[MAXNAME] = "user.txt";
, because I use it in more then 3 functions.. and yes I'm trying to keep distance from global as much as I can, but as I said I do not have that experience and I'm trying to learn something new from all of you.
Thank you everyone.. I really appreciate everyone's help.. until the next matter Cheers !! :)
Does the code in your last post actually compile?

You should be using something like strcpy() to copy the string into info.name, not assignment.

Well I did create a snake game my last post was about that and now I was thinking to add in the game something like a premenu when the user should add a name and the name is saved on a file, after entering the game can be played and it has a score too so I decide to make a structure where it should be save both the name and the score... but since in 'C' I can't assign values in the structures definition I can only create 2 global variables for name and score, and to go on.
About my last post:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Info
{
    int score;
    char name[MAXNAME];
}info;

void newUser()
{
    BOOL reg = FALSE;
    char nUser[MAXNAME] = {0};
    char fname[MAXNAME] = "user.txt";
    info.name[MAXNAME] = *fname;
    FILE *file = fopen(info.name, "r+");
.
..
}


This code compiles without no errors and warnings, the only problem is that what I did is useless as
info.name[MAXNAME] = *fname
doesn't assign info.name the "user.txt", so yes to make this work strcpy may be a solution, I did not try it yet as I was so exited to see if it works with the global variables, but if I fail I'll try your solution jlb

Thanks for the help...
Part of the problem with that assignment is that fname is a local variable, which means that it will go out of scope when the function ends. Another part of the problem is the [MAXNAME] after the variable in that assignment, you're confusing the compiler with "invalid" syntax.

That code should look more like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXNAME 100

struct Info
{
    int score;
    char name[MAXNAME];
};

void newUser(struct Info* info);

int main()
{
    // Avoid global variables, learn to properly pass variables to and from your functions.
    struct Info info; 

    newUser(&info);

    printf("%s\n", info.name);
}

// Learn to properly pass variables to and from your functions as required.
void newUser(struct Info* info)
{
    // info is a pointer so you must use pointer notation.
    strcpy(info->name, "user.txt");
    printf("%s\n\n", info->name);
}


Edit: Also if you're working on a C program you should mention that when you create the topic, as most people will assume that you are working on a C++ program, since this is a C++ forum.

Last edited on
Ahh ok ok understood, next time I will specify it.

In the meantime I found the solution I was looking for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#define MAXNAME 20

struct INFO
{
    int score;
    char user[MAXNAME];
};
.
..
void newUser()
{
    struct INFO info;
    int found = 0;
    char fname[MAXNAME] = "user.txt";
    char tmp[MAXNAME];
    FILE *file = fopen(fname, "a+");

    system("cls");
    printf("\n\n\n\n\t\t\t ~~~~~~ SnAkE GaMe ~~~~~~\n");
    printf("\n\n\n\n\n\t\t\t       Name:  ");
    scanf("%s", tmp);

    while(fscanf(file, "%19s", info.user) == 1)
    {
        if(strcmp(tmp, info.user) == 0)
        {
            found = 1;
            printf("\n\n\n\t\t\tSorry name already exists");
            Sleep(1500);
            fclose(file);
            break;
        }
    }
    if(!found)
    {
        fprintf(file, "%s\n", tmp);
        fclose(file);
        menu();
    }
}

For me works 100%
Thanks everyone and sorry for my bads, I'm a beginner not much to ask from me, and try to understand me as once you were in my spot.

Until next time cheers :)
Topic archived. No new replies allowed.