Thread: Beginner: Having trouble getting an input from a .txt file into a string

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    14

    Unhappy Beginner: Having trouble getting an input from a .txt file into a string

    Hi, wondering if anyone out there can help me with this.
    I'm learning C and having a bit of trouble getting to grips with taking inputs from external files.
    I made a .txt file, test.txt, which simly contains the word: working

    I'm now trying to make a program in C that will open the txt file, read the word "working" into a string and print it back to me on the screen.
    Here's the code I have so far:

    Code:
    /*	A program to test file input using C */
    
    #include "stdio.h"
    #include "string.h"
    
    int main(void)
    {
    	/* Open test.txt file for reading */
    
    	FILE *fp;
    	fp=fopen("c:\\test.txt", "r");
    	//fprintf(fp, "Testing...\n");
    
    	/* Declare a string */
    	
    	char string[20];
    
    	/* Input data from file into string */
    
    	fgets(string, 20, fp);
    
    	/* Print string */
    
    	printf("This program is: ");
    	printf( "String: %s", string);
    
    	/* Wait for user's key press, then close */
    
    	return 0;
    }
    The program has no trouble opening the file, but when I declare the string the errors begin... Anyone know where I'm going wrong here?
    If it helps, here are the errors I'm coming up with:
    Code:
    : error C2143: syntax error : missing ';' before 'type'
    : error C2065: 'string' : undeclared identifier
    : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
    : warning C4024: 'fgets' : different types for formal and actual parameter 1
    Once I get the hang of this I was hoping to be able to read from a list in a txt file (e.g. a list of names) and put each name into a string. If anyone could point me in the right direction with that as well it would be great

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    move declarations before other code in the function. it is not a C++ where you can mix declarations and other statements
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    ah, brilliant! Thanks for your help Vart!
    sorry, stupid mistake.
    There's one other thing you might be able to help me with:
    If I have a txt file with a list of names (each line having a persons first and last name seperated by a tab)
    e.g. John Smith
    Is there a way to stop the string when it reads the 'tab' rather than a NULL value that I think is supposed to stop it? And will the next string start reading the file where the last one left off?

    If anyone has any links to sites with good beginner info about file input using C it would be helpful, any information at all really. I've worked through the CProgramming.com tutorial but just have a few extra questions

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    read full line then use strtok or 2 other methods suggested by Dave_Shinkula http://cboard.cprogramming.com/showt...strtok+parsing
    to split the line into tokens
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Had a go with strtok. It works great! Just what I needed
    Thanks again for all your help!!

  6. #6
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Code:
    #include "stdio.h"
    #include "string.h"
    could be changed to
    Code:
    #include <stdio.h>
    #include <string.h>
    the <> tell the compiler to look in the include dir and the "" tell the compiler to look in the immediate dir

    http://www.cprogramming.com/tutorial/c/lesson1.html

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    ah, wasn't aware of that
    thanks for the advice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Trouble with file input
    By w274v in forum C Programming
    Replies: 6
    Last Post: 12-18-2005, 04:40 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM