Thread: Problem retrieving information from a file line by line.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    69

    Problem retrieving information from a file line by line.

    Well, I'm trying to scan though a file 'subdomains.txt' and print out what's on each line seperately. E.g.
    file contains:
    Code:
    this
    file
    means
    nothing_to_you
    I want each line to be printed out. Here's what I've got so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <tchar.h>
    
    
    void retrieveSubdomains(void);
    
    
    int main(int argc, char *argv[])
    {
        
        retrieveSubdomains();
        system("pause");
        return 0;
    }
    
    
    
    
    void retrieveSubdomains()  //return subdomain
    {
        TCHAR *filename = "subdomains.txt";
        FILE *fp = fopen(filename, "r"); /* _tfopen_s */
        TCHAR subs;
        TCHAR *fullSub = malloc(150*sizeof(TCHAR)); /* Couldn't be bothered with using realloc to update the size, so a set size will have to do. */
        short i = 0;
    
    
        if (fp != NULL)
        {
            while((subs = fgetc(fp)) != (TCHAR)feof)
            {
                while(subs != ' ' && subs != '\n')
                {
                    fullSub[i] = subs;
                    i++;
                }
    
    
                fullSub[i] = '\0';
                printf("%s\n", fullSub);
                //free(fullSub);
            }
        }else{
            fprintf(stderr, "Failed to open file '%s'!\n", filename);
        }
        free(fullSub);
    }
    My code crashes... Can you see what's wrong with it? Please and thank-you.
    Last edited by inu11byte; 11-11-2012 at 08:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-07-2012, 02:50 AM
  2. Retrieving a certain line from a large text file
    By Violet_Shift in forum C Programming
    Replies: 5
    Last Post: 05-06-2012, 11:37 AM
  3. Replies: 21
    Last Post: 08-07-2011, 09:55 PM
  4. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  5. Replies: 7
    Last Post: 12-13-2010, 02:13 PM