473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to use fscanf

56
I am trying to read the name, phone number and address from a file
here is the code but it doesn't work and i don't know where is the problem
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct PhoneBook{
  4.         char name[50];
  5.         int phoneNumber;
  6.         char address[50];
  7. } ;
  8. int read(struct PhoneBook user[], int n, FILE* fp);
  9.  
  10.  
  11. void main()
  12. {
  13.     //int i, j;
  14.     struct PhoneBook user[10];
  15.    FILE* fp = fopen("D:\\phoneBook.txt", "r");
  16.    int i = 0;
  17.    int counter;
  18.    while (!feof(fp)) {
  19.        read(user, i, fp);
  20.        i++;
  21.    }
  22.    counter = i;
  23. }
  24.  
  25. int read(struct PhoneBook user[], int n, FILE* fp){
  26.    //char temp;
  27.  
  28.    if(fp==NULL){
  29.        printf("Error\n");
  30.        return -1;
  31.    }
  32.    fscanf(fp,"%s %d %s\n", user[n].name,user[n].phoneNumber, 
  33.          user[n].address);
  34.  
  35. }
and here is the contents of the file:
jack 01014 jgd

Moh 02925 Tyeu
when i run it on dev c++, a message appear saying that the prog has stopped working and no output appears
i want to put the name in user[n].name, the phone number in user[n].phoneNumber and the address in user[n].address
Nov 28 '14 #1

✓ answered by Banfa

As with all the scanf family of functions you need to provide pointers to the locations to store data.strings, or arrays of charcters already decompose to a pointer by to write into the int you need to dereference it

Expand|Select|Wrap|Line Numbers
  1. fscanf(fp,"%s %d %s\n", user[n].name, &user[n].phoneNumber, user[n].address);
  2.  

1 1622
Banfa
9,065 Expert Mod 8TB
As with all the scanf family of functions you need to provide pointers to the locations to store data.strings, or arrays of charcters already decompose to a pointer by to write into the int you need to dereference it

Expand|Select|Wrap|Line Numbers
  1. fscanf(fp,"%s %d %s\n", user[n].name, &user[n].phoneNumber, user[n].address);
  2.  
Nov 28 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Benedicte | last post by:
Hi, I'm getting some problems when using fscanf to read a file. This is a piece of the program code: main () { /*** Variable declaration ***/ FILE *vpfile; /*** Data file ***/
4
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
9
by: quyvle | last post by:
I can't seem to get this function to work correctly. I'm wondering if anyone could help me out with this. So I'm using the fscanf function to read the input stream and store each string in the...
9
by: kvnsmnsn | last post by:
Over the course of my career I've transitioned from an Ada programmer (am I dating myself?) to a C programmer to a Java programmer and now back to a C programmer with the job I've currently...
37
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
59
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading...
1
momotaro
by: momotaro | last post by:
I have a small problem with the last fscanf in this function...every thing is logic in there but can't find the problem... plz help node *BuildGraph() { int range, vehicules, i, j,...
42
by: Bill Cunningham | last post by:
I'm doing something wrong and all I know to do is turn to clc. I have a text file containing 2 doubles separated by a tab. ..26 0 Is the text. I want to read the two double and printf them...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.