Cannot read string from a file!

This is a discussion on Cannot read string from a file! within the C Programming forums, part of the General Programming Boards category; Please help me in here. I have a file in which are written numbers and strings, like this: 12 MJ ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    17

    Cannot read string from a file!

    Please help me in here.

    I have a file in which are written numbers and strings, like this:
    12 MJ
    5 SK
    11 JO
    2 RE

    So i need to put the numbers (only the numbers!) in one array, and the strings in another one, coz the number of the numbers and the strings is equal.
    I tried several ways but it doesn't work - i got only 1 0 1 0 1 0 1 0, in another way 1 (null) 1 (null) 1 (null)... and in some way just some 1 and one small number (-12309343) and segmentation fault error.
    What is the problem? I realy can't figure it out. If you have any ideas about this, please help me.
    Thank you.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would probably help if you posted what you have tried.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    17
    Okay.

    1- fscanf(file, "%d %s", &m[i], buf[i][]);

    2- fscanf(file, "%d %s", &m[i], &buf[i]);

    3- fscanf(file, "%d %c", &m[i], buf[i][]);

    The [i] is coz the code is in a for loop.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps a bit more code would help. You almost certainly should not have the empty [] at the end of your buf[i], but exactly what you SHOULD have will depend on what your buf declaration is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Door number 2 is the closest, given a suitable 2D array of chars to work with for buf
    And drop the & from buf.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    17
    Here is my complete code:

    Code:
    #include <stdio.h>
    
    #define MAX 100
    
    void main()
    {
    int x[MAX];
    char y[MAX], z[MAX];
    FILE *file;
    int i, j;
    
    file = fopen("input", "r");
    
    fscanf("%i", &j);
    
    for(i = 0;i <= j;i++)
    {
    fscanf(file, "%i %c%c", &x[i], y[i], z[i]);
    printf("%i-%c%c\n", x[i], y[i], z[i]);
    }
    fclose(file);
    }
    It should read a file input:
    (Example):
    4
    2 RP
    1 SE
    12 QK
    52 AB

    , and it should collect the numbers and the characters. How to do this ??

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you post your code, and it produces wrong results, please tell us what those results are. It just saves time.



    Code:
    #include <stdio.h>
    
    #define MAX 100
    
    int main()
    {
      FILE *file;   
      char y[MAX], z[MAX];
      int i, j;
      int x[MAX];
      
    
      file = fopen("text.txt", "rt");
    
      fscanf(file, "%d", &j);
    
      for(i = 0; i < j; i++)
      {
         fscanf(file, "%d %c%c", &x[i], &y[i], &z[i]);
         printf("%d-%c%c\n", x[i], y[i], z[i]);
      }
      fclose(file);
      i = getchar();
      return 0;
    }
    /*
    It should read a file input:
    (Example):
    4
    2 RP
    1 SE
    12 QK
    52 AB
    */
    Last edited by Adak; 02-27-2009 at 05:44 PM.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    17

    Smile

    Quote Originally Posted by Adak View Post
    When you post your code, and it produces wrong results, please tell us what those results are. It just saves time.



    Code:
    #include <stdio.h>
    
    #define MAX 100
    
    int main()
    {
      FILE *file;   
      char y[MAX], z[MAX];
      int i, j;
      int x[MAX];
      
    
      file = fopen("text.txt", "rt");
    
      fscanf(file, "%d", &j);
    
      for(i = 0; i < j; i++)
      {
         fscanf(file, "%d %c%c", &x[i], &y[i], &z[i]);
         printf("%d-%c%c\n", x[i], y[i], z[i]);
      }
      fclose(file);
      i = getchar();
      return 0;
    }
    /*
    It should read a file input:
    (Example):
    4
    2 RP
    1 SE
    12 QK
    52 AB
    */
    Thank you so much!
    It seems like the fopen("txt", "rt) has been the problem. Now it's all okay. Thank you once again!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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