Thread: Return File Contents as String

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    4

    Return File Contents as String

    Hello,


    I am completely new to C programming. Below code is that I gathered from Google searches and maybe incorrect.


    I am trying to code a function which will read a file on system and return its content back as string. Code is below.


    Code:
    char * readtxt(){
        FILE * fptr;
        char  c;
        static char txt[30];
        int len=0;
    
    
        fptr = fopen("C:\\Users\\Test\\Desktop\\Dev\\test.txt", "r");
        
        while(1){
            c = fgetc(fptr);
            if(c!= EOF) {
                txt[len]=c;
                len++;
            }
            else
                break;
        }
        fclose(fptr);  
    
    
        return txt;  
    }

    I suppose txt variable is pointer. But I need to return the file content as string so the function structure should look like


    Code:
    returnType function(){
    return "File Contents as String";
    }

    How can I achieve this please?


    Cheers

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You need to find out the size of the file using fseek and ftell, and then dynamically allocate memory using malloc, and then read the contents of the file into the allocated memory.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    Quote Originally Posted by Elkvis View Post
    You need to find out the size of the file using fseek and ftell, and then dynamically allocate memory using malloc, and then read the contents of the file into the allocated memory.
    Hi,

    Thank you very much for the reply.

    Any code example please. I am really newbie and having difficulty to create C code listing.

    Cheers

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,068
    Also here -> Return File Contents
    @OP - you should stick to one forum at once, at least while you're making some progress over there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    Quote Originally Posted by Salem View Post
    Also here -> Return File Contents
    @OP - you should stick to one forum at once, at least while you're making some progress over there.
    Sorry but I want to stick anywhere I want. Your reply is not related to my question and topic.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,068
    Here, read this as well then
    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    Quote Originally Posted by Salem View Post
    Here, read this as well then
    How To Ask Questions The Smart Way
    I will have to report you if you do not cease flooding my question!

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by NecNecco View Post
    Sorry but I want to stick anywhere I want.
    Of course you're free to go around the internet and post the question on every forum, that is your right. However many will see this as bad form and call you out on it, which is what's happening now.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    Did you read the link provided in post #6? And by the way I find both posts very relevant to your problem.

    As to your actual question, I suggest you stop "Googling for the answer" and start writing your own code, then you may actually learn to program.

    Jim

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I will have to report you if you do not cease flooding my question!
    O_O

    Now you've done it, Salem; you've crossed the wrong man.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by NecNecco View Post
    Sorry but I want to stick anywhere I want. Your reply is not related to my question and topic.
    I'll tell you where you can stick it! Just kidding...sort of. There are at least a couple of reasons you should not cross-post (i.e. post in multiple forums). I'm not trying to be an a-hole or patronizing, rather to point out some issues with cross-posting you may not have been aware of.

    First, and most simply, it's against the forum rules (see this link, #3 in particular). When you signed up for an account, you agreed to read and abide by those rules. Simply put, you broke the rules and violated the terms of your membership. Salem as a moderator (one who is responsible for maintaining order in the forum), is perfectly justified in his response in #4. He simply reminded you of the rules you apprently forgot, misunderstood or outright ignored. If you really don't like our rules, disagree with them, or simply don't care to follow them, you're more than welcome to not use our forum. But if you want to use our forum, and our free service of (IMNSHO) excellent advice/help, you should consider showing more respect and courtesy to the members whom you seek help from and the moderators who keep this forum so effective and useful.

    Second, it results in a lot of duplicated effort. We may spend our time and effort -- which we volunteer for free, despite having jobs, school, families, etc -- when you don't really need our help since somebody on another forum already answered your question. Effectively, it's being disrespectful of those who are trying to help you. As an analogy, you wouldn't call several pizza delivery places, order a pizza from each, and only pay for the first pizza that arrives. Why would you treat our time and effort with that kind of disregard?

    We like to help people -- as I mentioned, we don't get paid, we volunteer what little spare time and energy we have -- and one of the ways we can make sure we can continue to be helpful to everybody, is by ensuring people follow forum guidelines and etiquette. I suggest you read the following link (don't take the title personally): How To Ask Questions The Smart Way. It has a lot of great advice as to how to get the most out of your forum and mailing list experiences.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by NecNecco View Post
    I will have to report you if you do not cease flooding my question!
    Oh, the irony. You realize you would report such an "offense" to a moderator. Do you also realize that Salem is a moderator. And do you realize that he hasn't broken any rules, unlike you?

    Reminder, you broke the rules. Your "reporting" Salem for explaining the rules you broke and how not to break the rules, is like trying to report a police officer who pulled you over for running a red light, because you think that red lights shouldn't apply to you and the police officer shouldn't be enforcing the law.

  13. #13
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    86
    Quote Originally Posted by NecNecco View Post
    I will have to report you if you do not cease flooding my question!
    Feeling a bit entitled, are we? lol
    V8 Interceptor: KDE Neon 5.15
    Steering wheel: gcc 7.3.0 in Code::Blocks 16.01
    Supercharger: NASM 2.13.02
    Engine: AMD Ryzen 7 1700
    Dashboard: NVIDIA GeForce GTX 1060 6GB
    Rusty old trailer for hauling 50% of my Steam games: Windows 7 Pro 64bit SP1
    Roach-infested ORV for Bandlab Sonar: Windows 7 Pro 64bit SP1

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,875
    When cross posting, you should state where it is cross posted on this forum and the other. We can then go there and see what answers you have received so far.

    You should then state why you have done this, such as, "I have received no replies".

    "I want to get as many resposes as possible" is not a good excuse to cross post.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-24-2013, 05:12 PM
  2. Replies: 2
    Last Post: 03-08-2009, 08:54 PM
  3. C++ string contents
    By Smiley10 in forum C++ Programming
    Replies: 13
    Last Post: 08-23-2007, 09:12 AM
  4. storing text file contents into a string
    By m.mixon in forum C Programming
    Replies: 4
    Last Post: 07-20-2006, 11:52 AM
  5. Replies: 6
    Last Post: 04-09-2006, 04:32 PM

Tags for this Thread