Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-03-2004, 04:56 AM   #1 (permalink)
toblerone
Registered User
 
toblerone's Avatar
 
Join Date: Jul 2004
Posts: 11
toblerone is on a distinguished road
Talking text file recursive string reading C

have V.little knowledge of files
need to know if it is possible to read a line from a text file, print it etc, then read the next line in the text file
I am using Visual C v6 on windows 98

Another thing whats the format for writing and declaring a function protoype the returns more than one object?

Last edited by toblerone; 08-03-2004 at 06:23 AM.
toblerone is offline   Reply With Quote
Old 08-03-2004, 10:29 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Does it have to be in "C"? Not C++?
__________________
Valmont is offline   Reply With Quote
Old 08-04-2004, 01:17 AM   #3 (permalink)
toblerone
Registered User
 
toblerone's Avatar
 
Join Date: Jul 2004
Posts: 11
toblerone is on a distinguished road
Sorry its defonately boring plain ol' "C"
It dosent HAVE to be a text file though, basically I need to be able to cycle thru the individual strings of different files (thou of the same file type)
as it is I may have to write out 2d arrays in place of each file...this could take donkeys years!

Last edited by toblerone; 08-04-2004 at 03:12 AM.
toblerone is offline   Reply With Quote
Old 08-04-2004, 01:09 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Hmm, then you'll have to wait for a "C" guy to show up . I don't do C.
__________________
Valmont is offline   Reply With Quote
Old 08-04-2004, 11:05 PM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
A small program to read lines from a file:
(Note program has not been testet, not even compiled)
Code:
/* 
 * needed includes in order to use:
 * fopen(), fgets(), fclose() and printf()
 */
#include <stdio.h>

/*
 * In order to have a precise line length we're going for everytime
 * we define the LINE_SIZE
 */
#define LINE_SIZE 256

/*
 * Definition of function beeing used by main(),
 * also known as the prototype.
 */
int read_file(char* file_name);

/*
 * main() is a function which is invoked with every argument given
 * in the program call.
 * argc: tells you how many arguments given (including program name)
 * argv: an array of char* with the arguments parsed to the program
 */
int main(int argc, char* argv[])
{
  int status = 0;
  if(argc != 2)
  {
    printf("Usage: %s <file_name>\n", 
            argv[0]);
    return -1;
  {
  if(0 != (status = read_file(argv[1]))
    printf("Reading of file [%s] failed with status: %d\n", 
            argv[1], status);
  return status;
}

/*
 * read_file() is the actual function to open/read and print
 * what has been read from the file.
 * Depending on where it might fail, it will return an error code
 * to which the caller can act apropriate on.
 */
int read_file(char* file_name)
{
  FILE* file;
  char buffer[LINE_SIZE];
  int i = 0;
  file = fopen(file_name);
  if(!file)
    return -1;
  while(!feof(file))
  {
      if(!fgets(buffer, LINE_SIZE, file))
      {
         fclose(file);
         return -2;
      }
      printf("Line (%d) in file [%s]: %s\n",
              ++i, file_name, buffer);
  }
  fclose(file);
  return 0;
}
Quote:
Another thing whats the format for writing and declaring a function prototype the returns more than one object?
In short you can't.
C dosn't allow you to return different types of objects, the function prototype can be declared to return a specific struct, and this can consist of pointers to different objects/structs/types, where the function can either set these to NULL or point to the resulting object.

An ugly way of achieving it, is to have a function which returns a void* and then later on typecast that to whatever you would use, but that's just insane, since a void* wont tell you what type it intentionaly was.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 08-05-2004 at 12:00 AM.
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
From C to Java HighterDK Java 11 07-13-2004 08:15 PM
edit? anon Lounge 10 11-21-2002 04:02 PM


All times are GMT -8. The time now is 05:11 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting