Thread: fscanf() and reading a file ,which has unknown line , line by line

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    46

    fscanf() and reading a file ,which has unknown line , line by line

    hi guys a have a problem with my homework.I need to read a file line by line with fscanf() but i couldnt do it . the content of the file is as shown:
    Madrid Rome 300
    Prague Paris 500
    Istanbul Rome 350
    Paris Rome 600
    .
    .
    .

    And i need to read line by line. how could i do it ? but i'm not allowed to use static arrays ?

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    I don't really know what you mean by line by line but maybe you can look every char unetil you find \n .
    Last edited by dekl; 06-06-2012 at 02:26 PM.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    46
    Quote Originally Posted by dekl View Post
    I don't really know what you mean by line by line but maybe you can look every char unetil you find \n .
    I've though i know but ı couldnt do it .

    here is my code
    Code:
    
    while (fscanf(ifp,"%s %s %d",city1,city2,distance) != EOF) {
      
    }
    printf("%s",city1);
      printf("\n");
      printf("%s",city2);
      printf("\n");
    but it gives me city1=600 and city2=Rome instead of madrid and rome .where ı am wrong ?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What do you need to do with the data you read? Do you need to store it or do some type of processing on it?

    [edit]You probably need to use the address of operator (&) to store into the distance variable.[/edit]

    [edit2]You probably want the print commands to be inside the loop, not outside of it.[/edit2]
    Last edited by hk_mp5kpdw; 06-06-2012 at 02:32 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    46
    I've tried to make shortest path algorithm and i need to read cities and distances between them from a file .

    and you are right ,I forgot the &
    Last edited by dayanike; 06-06-2012 at 02:35 PM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by hk_mp5kpdw View Post
    [edit2]You probably want the print commands to be inside the loop, not outside of it.[/edit2]
    Did you heed this advice? "Madrid" and "Rome" are stored during the first iteration of the "while" loop, but are overwritten during subsequent iterations. And nothing is printed until the loop is complete. This might also highlight where your data read gets fluky.

  7. #7
    Registered User Sokion's Avatar
    Join Date
    Jun 2012
    Posts
    26
    Well, for what its worth, you can try to finish the format argument with "\n" to read it line by line. It just tells the function to eat an End-of-Line character and then continue (if it is in a loop)...

    Don't you just hate homework. I am stuck at a similar thing, but with the actual file I/O...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  2. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 12:34 AM
  3. Reading Input from a file, line-by-line
    By Acolyte in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 01:03 PM
  4. Replies: 1
    Last Post: 05-20-2006, 11:17 PM
  5. reading a file line by line and char *
    By odysseus.lost in forum C Programming
    Replies: 8
    Last Post: 05-31-2005, 09:47 AM