Problem retrieving information from a file line by line.

This is a discussion on Problem retrieving information from a file line by line. within the C Programming forums, part of the General Programming Boards category; Well, I'm trying to scan though a file 'subdomains.txt' and print out what's on each line seperately. E.g. file contains: ...

  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 07:39 PM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    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 && subs != ' ' && subs != '\n')
    			{
    				printf("%c", fullSub[i] = subs);
    				printf("%d", strlen(fullSub));
    				i++;
    			}
    
    
    			fullSub[i] = '\0';
    			printf("%d\n", sizeof *fullSub);
    			//free(fullSub);
        }else{
            fprintf(stderr, "Failed to open file '%s'!\n", filename);
        }
    	for(i=0;i<strlen(fullSub);i++)
    		printf("%c", fullSub[i]);
    		
    	free(fullSub);
    }
    Thant's my current code, but I am having some problems, still. Strlen returns 6..? And I need to know how to jump to the next line? Would I just increment 'i' when I reach the newline character and it'll jump to the next line?

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by inu11byte View Post
    Code:
    			while((subs = fgetc(fp)) != (TCHAR)feof && subs != ' ' && subs != '\n')
    You shouldn't use casts unless you understand why they're needed. Using them to hide compiler warnings is not a good reason (and yes, you should turn on and pay attention to all compiler warnings).

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by christop View Post
    You shouldn't use casts unless you understand why they're needed. Using them to hide compiler warnings is not a good reason (and yes, you should turn on and pay attention to all compiler warnings).
    Well... you're right, I shouldn't cast it like that. I'll change the while to:
    Code:
    while((subs = fgetc(fp)) != ' ' && subs != '\n' && !feof(fp))
    Any ideas on why I'm get unexpected results? Or more improvements I could make?
    Last edited by inu11byte; 11-11-2012 at 09:38 PM. Reason: Grammar

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by inu11byte View Post
    Any ideas on why I'm get unexpected results? Or more improvements I could make?
    What are your unexpected results?

    Right now, you're only reading the first line because of your while condition which stops when fgetc() reads in the newline character from the first line.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You should look into using fgets(...) since it reads one line at a time.

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, 09:51 AM
  5. Replies: 7
    Last Post: 12-13-2010, 01:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21