Jump to content

C: user defined strings and files

Go to solution Solved by Avratz,

Take address1 out of quotes in your fopen_s call. You're trying to open a file called "address1", not what you entered.

 

fopen_s(&file_in, address1, "r");

hi guys,

you're all so smart so I'm back again to ask where I'm going wrong (cause i really don't know and I've searched the web and haven't found anything thus far)

 

so i've opened a pre-defined file using fopen_s. I need to open a user-defined string. will not work right now D:

 

check it out for me please :)?

 

code (some of which is a template for later things):

//This program is made for the purpose of text file analysis and merging of text files//This program was first created 3/12/2014//Made by Laurence Potter LP379#include <stdio.h>#include <stdlib.h>#include <string.h>    int main(void) {errno_t err;FILE *file_in; char ccount[5000], address1[200], address2[200]; printf("Please enter in the address of the text file you wish to open.\nPlease remember to 'escape' your backslashes: \n ");gets_s(address1, 199);printf("\nPlease enter in a second address of the text file you wish to open.\nPlease remember to 'escape' your backslashes: \n ");gets_s(address2, 199);printf("\n..."); err = fopen_s(&file_in, address1, "r");if (err == 0){printf("The file '%s' was opened\n", address1);}else{printf("The file '%s' was not opened\n", address1);} if (file_in){err = fclose(file_in);if (err == 0){printf("The file '%s' was closed\n", address1);}else{printf("The file '%s' was not closed\n", address1);}return 0;}} 

post-16255-0-46115700-1417644547_thumb.p

Spoiler

Gaming/Engineering PC: -i7 6700K, 4-4.2GHz "Eleanor" -ASUS ROG HERO VIII MOBO -16GB DDR4 3000MHz Corsair (2x8GB) -Gigabyte Windforce 980Ti OC edition (1405MHz GPU clock) -H110i GT Corsair CPU Water cooler -980GB Sandisk Ultra II SSD -Corsair 450D ATX Case -RM850i Corsair PSU (Modular) -28” 4K Samsung -27” 1080p Samsung 

Link to comment
Share on other sites

Link to post
Share on other sites

please format that code to look nicer :)

 

at

err = fopen_s(&file_in, address1, "r");

there is no need for &

Link to comment
Share on other sites

Link to post
Share on other sites

please format that code to look nicer :)

 

at

err = fopen_s(&file_in, address1, "r");

there is no need for &

I get this error when doing your amendment

post-16255-0-94446300-1417644992_thumb.p

Spoiler

Gaming/Engineering PC: -i7 6700K, 4-4.2GHz "Eleanor" -ASUS ROG HERO VIII MOBO -16GB DDR4 3000MHz Corsair (2x8GB) -Gigabyte Windforce 980Ti OC edition (1405MHz GPU clock) -H110i GT Corsair CPU Water cooler -980GB Sandisk Ultra II SSD -Corsair 450D ATX Case -RM850i Corsair PSU (Modular) -28” 4K Samsung -27” 1080p Samsung 

Link to comment
Share on other sites

Link to post
Share on other sites

lol I missed one thing. Why you have file_in parameter in there?

 

fopen(const char *filename, const char *mode)

 

source

Link to comment
Share on other sites

Link to post
Share on other sites

lol I missed one thing. Why you have file_in parameter in there?

 

fopen(const char *filename, const char *mode)

 

source

microsoft being microsoft decided to make things complicated by introducing a 'safer' version of fopen (and scanf etc) by putting '_s' on the end of them. This changes the format of the arguments and generally makes less sense (or at least to me) in comparison to the standard fopen.

 

now there are ways to get around the _s bit, but as this is for a university assignment, I'm trying to avoid that due to the fact that i suspect that they want me to use the more 'secure' fopen variety...

 

thanks for your help though

Spoiler

Gaming/Engineering PC: -i7 6700K, 4-4.2GHz "Eleanor" -ASUS ROG HERO VIII MOBO -16GB DDR4 3000MHz Corsair (2x8GB) -Gigabyte Windforce 980Ti OC edition (1405MHz GPU clock) -H110i GT Corsair CPU Water cooler -980GB Sandisk Ultra II SSD -Corsair 450D ATX Case -RM850i Corsair PSU (Modular) -28” 4K Samsung -27” 1080p Samsung 

Link to comment
Share on other sites

Link to post
Share on other sites

microsoft being microsoft decided to make things complicated by introducing a 'safer' version of fopen (and scanf etc) by putting '_s' on the end of them. This changes the format of the arguments and generally makes less sense (or at least to me) in comparison to the standard fopen.

 

now there are ways to get around the _s bit, but as this is for a university assignment, I'm trying to avoid that due to the fact that i suspect that they want me to use the more 'secure' fopen variety...

 

thanks for your help though

oh thats other story. Its late here where I am so I oversaw that _s :)

Link to comment
Share on other sites

Link to post
Share on other sites

This is still an unsolved issue for anyone reading

Spoiler

Gaming/Engineering PC: -i7 6700K, 4-4.2GHz "Eleanor" -ASUS ROG HERO VIII MOBO -16GB DDR4 3000MHz Corsair (2x8GB) -Gigabyte Windforce 980Ti OC edition (1405MHz GPU clock) -H110i GT Corsair CPU Water cooler -980GB Sandisk Ultra II SSD -Corsair 450D ATX Case -RM850i Corsair PSU (Modular) -28” 4K Samsung -27” 1080p Samsung 

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe try to take out the escape \ from the input. I know that you need a \\ to get a \ in C. However, looking at the output, it looks like it is not working at expected due to the string prints out like "home\\Documents\\...".

My guess is that get_s may remove the need to use the escape \ due to the fact normal users would have no clue that they need to type \\.  

Link to comment
Share on other sites

Link to post
Share on other sites

Escaping is only needed for string literals. User input doesn't need to. In your original example, you had the file paths hardcoded, and thus needed to escape them.

 

The *_s functions are done that way, so the function can return a meaningful error value. It's safer in the sense that you are handed the error code directly rather than relying on a global error flag. See the section on return values here http://msdn.microsoft.com/en-us/library/z5hh6ee9.aspx

 

For standard fopen you have to make a call to perror() or check the value of errno to get more information on the error. A NULL file handle isn't very meaningful beyond the fact it failed. Sometimes it's useful to know why.

 

ex

 

#include <stdio.h>#include <string.h>intmain(void){    FILE* fp;    char fname[128];    fgets(fname, 128, stdin);    fname[strlen(fname) - 1] = '\0';    fp = fopen(fname, "r");    if(!fp)    {        char buf[256];        sprintf(buf, "Failed to open %s ", fname);        perror(buf);        return 0;    }    char fbuf[128];    fgets(fbuf, 128, fp);    puts(fbuf);    fclose(fp);    return 0;} 

 

6QDQtqw.png

 

 

Microsoft version

 

#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]){    FILE* fp;    char fname[128];    fgets(fname, 128, stdin);    fname[strlen(fname) - 1] = '\0';    errno_t err;    err = fopen_s(&fp, fname, "r");    if (err)    {        char buf[256];        strerror_s(buf, 256, err);                fprintf(stderr, "Failed to open %s : %s", fname, buf);        return 0;    }    char fbuf[128];    fgets(fbuf, 128, fp);    puts(fbuf);    fclose(fp);	return 0;} 

 

mIX3U7a.png

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

Escaping is only needed for string literals. User input doesn't need to. In your original example, you had the file paths hardcoded, and thus needed to escape them.

 

The *_s functions are done that way, so the function can return a meaningful error value. It's safer in the sense that you are handed the error code directly rather than relying on a global error flag. See the section on return values here http://msdn.microsoft.com/en-us/library/z5hh6ee9.aspx

 

For standard fopen you have to make a call to perror() or check the value of errno to get more information on the error. A NULL file handle isn't very meaningful beyond the fact it failed. Sometimes it's useful to know why.

 

ex

 

#include <stdio.h>#include <string.h>intmain(void){    FILE* fp;    char fname[128];    fgets(fname, 128, stdin);    fname[strlen(fname) - 1] = '\0';    fp = fopen(fname, "r");    if(!fp)    {        char buf[256];        sprintf(buf, "Failed to open %s ", fname);        perror(buf);        return 0;    }    char fbuf[128];    fgets(fbuf, 128, fp);    puts(fbuf);    fclose(fp);    return 0;} 

 

6QDQtqw.png

 

 

Microsoft version

 

#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]){    FILE* fp;    char fname[128];    fgets(fname, 128, stdin);    fname[strlen(fname) - 1] = '\0';    errno_t err;    err = fopen_s(&fp, fname, "r");    if (err)    {        char buf[256];        strerror_s(buf, 256, err);                fprintf(stderr, "Failed to open %s : %s", fname, buf);        return 0;    }    char fbuf[128];    fgets(fbuf, 128, fp);    puts(fbuf);    fclose(fp);	return 0;} 

 

mIX3U7a.png

 

a lot of this is using syntax's which i haven't learnt/seen before.. thanks for showing me these, but i don't think i can use these as this is a uni assignment, so i can only really use what they've taught me... (though they haven't ever shown me how to put a string into a fopen_s yet

 

 

 

 

Maybe try to take out the escape \ from the input. I know that you need a \\ to get a \ in C. However, looking at the output, it looks like it is not working at expected due to the string prints out like "home\\Documents\\...".

My guess is that get_s may remove the need to use the escape \ due to the fact normal users would have no clue that they need to type \\.  

 

 

 

hmm, still not doing it :S

 

post-16255-0-20808000-1417701430_thumb.p

 

 

both screenshots show that a static address will show up the file as opened, but a user defined won't work... 

post-16255-0-33505900-1417701053_thumb.p

Edited by iWearKiltz
Spoiler

Gaming/Engineering PC: -i7 6700K, 4-4.2GHz "Eleanor" -ASUS ROG HERO VIII MOBO -16GB DDR4 3000MHz Corsair (2x8GB) -Gigabyte Windforce 980Ti OC edition (1405MHz GPU clock) -H110i GT Corsair CPU Water cooler -980GB Sandisk Ultra II SSD -Corsair 450D ATX Case -RM850i Corsair PSU (Modular) -28” 4K Samsung -27” 1080p Samsung 

Link to comment
Share on other sites

Link to post
Share on other sites

Take address1 out of quotes in your fopen_s call. You're trying to open a file called "address1", not what you entered.

 

fopen_s(&file_in, address1, "r");

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

Take address1 out of quotes in your fopen_s call. You're trying to open a file called "address1", not what you entered.

 

fopen_s(&file_in, address1, "r");

me and you should become friends... or something similar

 

you've solved two of my problems now... have you done a c programming course/module?

Spoiler

Gaming/Engineering PC: -i7 6700K, 4-4.2GHz "Eleanor" -ASUS ROG HERO VIII MOBO -16GB DDR4 3000MHz Corsair (2x8GB) -Gigabyte Windforce 980Ti OC edition (1405MHz GPU clock) -H110i GT Corsair CPU Water cooler -980GB Sandisk Ultra II SSD -Corsair 450D ATX Case -RM850i Corsair PSU (Modular) -28” 4K Samsung -27” 1080p Samsung 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×