473,473 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problems with file reading and strings

20 New Member
Alright, as a continuation of my previous question, I've got two problems with this program-the file read only reads the last line of the input file, and, when I try to assign a tokened word to a spot in the array, it returns a segmentation fault. Anybody know what I'm missing?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main (void)
  5. {
  6.         FILE *file;
  7.         file = fopen("input2.txt", "r");
  8.  
  9.         char tokenarray[100];
  10.         char input[200];
  11.         char *tokenPtr;
  12.         int cnt;
  13.  
  14.         while(fgets(input, 100, file) != NULL)
  15.         {
  16.                 fscanf(file, "%s", input);
  17.         }
  18.         fclose(file);
  19.  
  20.         printf("%s", input);
  21.  
  22.         tokenPtr = strtok(input, " ");
  23.         while (tokenPtr != NULL)
  24.         {
  25.                 tokenPtr = strtok(NULL, " ");
  26.                 for (cnt = 0; cnt < 100; cnt ++);
  27.                 {
  28.                         *tokenPtr = tokenarray[cnt];
  29.                 }
  30.         }
  31.  
  32.         printf("%s", tokenarray);
  33.  
  34.         return 0;
  35. }
Feb 28 '07 #1
1 1169
horace1
1,510 Recognized Expert Top Contributor
you were reading the file, doing nothing with the input and then closing it. So when you when onto tokenising you had the last line of the file
Expand|Select|Wrap|Line Numbers
  1.         while(fgets(input, 100, file) != NULL)
  2.         {
  3.                 fscanf(file, "%s", input);
  4.         }
  5.         fclose(file);
  6. etc
  7.  
you then had problems with your pointers which caused segmentation errors
try this modified code
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main (void)
  5. {
  6.         FILE *file;
  7.         file = fopen("input2.txt", "r");
  8.        printf("start");
  9.         char tokenarray[100];
  10.         char input[200];
  11.         char *tokenPtr;
  12.         int cnt;
  13.  
  14.         while(fgets(input, 100, file) != NULL)
  15.         {
  16.                 fscanf(file, "%s", input);
  17.    //     }
  18.     //    fclose(file);
  19.  
  20.  
  21.         tokenPtr = strtok(input, " ");
  22.         printf("%s\n", tokenPtr);
  23.         while (tokenPtr != NULL)
  24.         {
  25.                 tokenPtr = strtok(NULL, " ");
  26.                // for (cnt = 0; cnt < 100; cnt ++);
  27.               //  {
  28.                //         *tokenPtr = tokenarray[cnt];
  29.                // }
  30.                printf("%s\n", tokenPtr);
  31.         }
  32.  
  33.         //printf("%s", tokenarray);
  34.        }
  35.         fclose(file);   // <<<< move file close to here
  36.         system("pause");
  37.         return 0;
  38. }
  39.  
it reads the file and displays tokens - it is not quite correct but you can now try and fix it
Feb 28 '07 #2

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

Similar topics

7
by: Stingray | last post by:
Are there any know problems with using a MemoryStream as a backing store for a CryptoStream? I'm trying to simply encrypt and decrypt text in memory. I'd like to create some simple methods to...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
7
by: Timo Haberkern | last post by:
Hi there, i have some troubles with my TSearch2 Installation. I have done this installation as described in http://www.sai.msu.su/~megera/oddmuse/index.cgi/Tsearch_V2_compound_words...
1
by: Galen Somerville | last post by:
And yet another VB6 to VB2005 problem. All helpful suggestions appreciated. As you can see in the code below, my structures use fixed length strings and known array sizes. Consequently I can save...
4
by: Gaijinco | last post by:
I had a file named nap.in which looks like this: 4 10:00 12:00 Lectures 12:00 13:00 Lunch, like always. 13:00 15:00 Boring lectures... 15:30 17:45 Reading 4 10:00 12:00 Lectures 12:00 13:00...
9
by: Sheldon | last post by:
Good day Everyone, I am a still very new at learning C and I have thrown myself in the deep end. I started with a simple program and kept widening the scope. This has taught me many things about...
2
by: ajay0419 | last post by:
Hi: I am trying to read from a file, which stores answers for a particular question(The answers are stop listed ). Each answer in the file is enclosed in opening and closing braces. Here is the...
9
by: dgleeson3 | last post by:
Hello All I have a txt file of strings of different lengths. I dont know how many strings are in the file. I have no problem reading the file and sending to the console (as below). To...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.