Thread: help with reading data file

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    44

    help with reading data file

    1234
    Last edited by loso44x; 10-02-2005 at 06:52 PM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    #include <string.h>
    
    ...
    ...
    ...
    
    char* p = strchr(string,'\n');
    
    if (p)
      *p = 0;
    edit: gah i keep doing that, also removed quotes around zero.

    thanks, quzah
    Last edited by nonpuz; 10-01-2005 at 08:39 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nonpuz
    Code:
    #include <string.h>
    
    ...
    ...
    ...
    
    char* p = strchr(string,'\n');
    
    if (p)
      p = '0';
    Minor error there. Compile that, and see what your compiler has to say.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    i edited the code to this
    Code:
        for (i=0;i<=SIZE;i++)
        {
              fscanf(in_file, "%35[^,],%10[^,],%10[^,],%s",movies[i].title, movies[i].rating,movies[i].date,&movies[i].length);              
              char* p = strchr(movies[i].length,'\n');
              if (p)
                 *p = 0;
                  
              printf("Name of Movie:%s \n Rating:%s \n  Date:%s \n Length:%s\n",movies[i].title, movies[i].rating,movies[i].date,movies[i].length);   
         }
    but i get the same results am i doing something wrong?

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Code:
    		for(j=k+1;j<=SIZE;j++) // j = k + 1 would make  it skip anything above it,
    if e.g. k = 5 or something then it will skip 0 to 4. J should start with 0.
    		{
    			if(strcmp(movies[k].title,movies[j].title) > 0) 
    			{
    				movies2=movies[k];
    				movies[k]=movies[j];
    				movies[j]=movies2;
    			}
    		}

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    Quote Originally Posted by loko
    Code:
    		for(j=k+1;j<=SIZE;j++) // j = k + 1 would make  it skip anything above it,
    if e.g. k = 5 or something then it will skip 0 to 4. J should start with 0.
    		{
    			if(strcmp(movies[k].title,movies[j].title) > 0) 
    			{
    				movies2=movies[k];
    				movies[k]=movies[j];
    				movies[j]=movies2;
    			}
    		}

    i changed j to 0 and it didnt sort.. i think changing it will just compare the same array

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    4567
    Last edited by loso44x; 10-02-2005 at 06:52 PM.

  8. #8
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    The SIZE of array is only 4 and you have 5 record in there.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    Quote Originally Posted by loko
    The SIZE of array is only 4 and you have 5 record in there.
    k i edited the code but there seems to be a problem with the sorting

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>printf("%s - %s - %s - %s",movies[k].title,movies[k].rating,movies[k].date,movies[k].length);
    This doesn't output any newline characters, you need a \n at the end of each line (I assume). Same for the other printf() statement too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    Quote Originally Posted by Hammer
    >>printf("%s - %s - %s - %s",movies[k].title,movies[k].rating,movies[k].date,movies[k].length);
    This doesn't output any newline characters, you need a \n at the end of each line (I assume). Same for the other printf() statement too.
    if i put a \n after each printf then some will have double space and some will have one space

  12. #12
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    You have a '\n' in the beggining of evry movie title except for the first move in the file.

    *edited

    try
    Code:
     for (i=0;i<SIZE;i++)
        {
              fscanf(in_file, "\n%35[^,],%10[^,],%10[^,],%3[^\n]",movies[i].title, movies[i].rating,movies[i].date,&movies[i].length);              
     
         }
    Last edited by loko; 10-02-2005 at 05:55 PM.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I expect that's what you get for using fscanf() I'm afraid. Unwanted data is being left in the buffer and confusing subsequent calls to fscanf().

    Try replacing the call to fscanf() with fgets(), followed by sscanf().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    Quote Originally Posted by loko
    You have a '\n' in the beggining of evry movie title except for the first move in the file.

    *edited

    try
    Code:
     for (i=0;i<SIZE;i++)
        {
              fscanf(in_file, "\n%35[^,],%10[^,],%10[^,],%3[^\n]",movies[i].title, movies[i].rating,movies[i].date,&movies[i].length);              
     
         }
    thx works like a charm now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. reading data from a file
    By brianptodd in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2003, 06:50 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM